R language ggplot2 drawing torus radar chart / star chart / polar chart / radial chart polar chart visual analysis of vehicle performance data

Time：2022-6-20

Beautiful circle chart. I’m not sure if there is any additional benefit to the data analysts themselves, but if it can attract the attention of decision makers, it is additional value to me.

However, with Coord\_ Polar() or coord in ggplot2 found occasionally\_ Radar () can be difficult to build. The two main problems I found are that the change of polar coordinates will bend your path into a circle, and the radar can’t communicate with geom_ Bin is used in conjunction to fill the background.

This is why I usually use Cartesian coordinates. More like a mathematical solution. Map your data and drawing requirements so that they eventually become circles. As an added benefit, I also found it to be faster to build / load. It’s important to me because I let them interact in shiny apps.

I used mtcars data in my example. The figure shows the 12 vehicles in the collection:

• Cylinder in background. 4. Light, medium and dark colors for cylinders 6 and 8.
• Mark the miles per gallon of each car in blue.

I use the first 12 cars and want a column that contains row names.

``add_rownames\[1:12,\]``

Draw data mapping

To map the values of any column I want to plot, I create a function. It basically checks how many variables you want to plot and plots sinusoids for X and Y values.

``````  lev <- levels
num <- length

dir <- rep
dir_ <- map_dbl
interp
data <- mutate_``````

Store mapping data to map mpg variables for all labels.

``````#Data point
rotate_data``````

I wanted to show the plot range data, so I forged a series of qseC data. Basically, you generate a data frame with multiple values (rows) for qseC on each vehicle (tag).

``````#Make up some range data
bind_rows
rotate_data``````

Use polygon to draw the range, and use path and point to draw the mpg value.

``````lim <- max
#Draw the pattern of each layer with your own data and aesthetics
ggplot() +
geom_polygon+
geom_point+
theme+
coord_equal``````

I guess the desired grid is made up of radial outward lines with circles. Create x, xend, y, and yend data points to draw segments between them.

``````r <- data_frame %>%
mutate
r\$xed <- 0
r\$yed <- 0``````
``````#Draw the pattern of each layer with your own data and aesthetics

ggplot() +
geom_segment +
geom_polygon+
geom_point +
ylim + xlim +
theme+
coord_equal``````

label

Add a text label to the variable you rotate.

``label <- levels``
``````ggplot() +
geom_segment +
geom_polygon +
geom_path +
geom_point +
geom_text+
theme +
coord_equal``````

Draw a circle

To draw a circle, I will use a circle with a fill option.

``````t <- seq
d <- data.frame
If (fed==true) {\\\\add a point in the center to fill the whole "pie"
d <- rbind
}
return(d)``````

Grid circles and labels

Circular gridlines are built by calling circle multiple times and storing all points in the data frame.

``````cregrd <- data_frame
crld <- cili %>%
mutate

poties <- bind_rows
loics\$lv <- as.factor``````

Circular labels can be added in a number of ways. But to simply set all axis text and axis labels to blank, I built a data box that can be drawn using text.

``````c <- data_frame
cl <- cl\[x <= max,\]``````
``````ggplot() +
geom_segment +
geom_path+
geom_text+
geom_polygon +
geom_point +
geom_text + xlim +
theme+
coord_equal``````

background

With circle, you can also easily build a circle section, which you can fill with the filled = true parameter. There is a little -1/num offset to make the part align correctly. Here, you propose the factor variable you want to color. Of course, you can also change the code to change the “height” of each bar based on variables.

``````num      <- length
levels   <- rev
tart    <- seq

bg  <- data_frame
mutate

bgdta <- tdf
bgdta\$lv <- as.factor``````

Central circle

Not much detail, but you may want to add some center circles.

``````lm <- max
ggplot() +
geom_polygon+
geom_segment +
geom_path+
geom_text +
geom_polygon+
geom_polygon+
geom_path +
geom_point +
geom_text +
ylim + xlim +
theme +
coord_equal``````

Most popular insights