R combat | Radar Chart
A radar chart , also known as a spider plot , is a chart that shows the strength of multidimensional data. It maps the amount of data in multiple dimensions to coordinate axes. These coordinate axes start from the same center point and usually end at the edge of the circle. Connecting the same group of points with a line is called a radar chart.
This article takes the R package fmsb
and ggradar
as an example to introduce the drawing of radar charts.
fmsb
install.packages("fmsb")
library(fmsb)
# 示例数据
exam_scores <- data.frame(
row.names = c("Student.1", "Student.2", "Student.3"),
Biology = c(7.9, 3.9, 9.4),
Physics = c(10, 20, 0),
Maths = c(3.7, 11.5, 2.5),
Sport = c(8.7, 20, 4),
English = c(7.9, 7.2, 12.4),
Geography = c(6.4, 10.5, 6.5),
Art = c(2.4, 0.2, 9.8),
Programming = c(0, 0, 20),
Music = c(20, 20, 20)
)
exam_scores
> exam_scores
Biology Physics Maths Sport English Geography Art Programming Music
Student.1 7.9 10 3.7 8.7 7.9 6.4 2.4 0 20
Student.2 3.9 20 11.5 20.0 7.2 10.5 0.2 0 20
Student.3 9.4 0 2.5 4.0 12.4 6.5 9.8 20 20
data preparation
Data requirements:
Row 1 must contain the maximum value of each variable
Line 2 must contain the minimum value of each variable
The number of columns or variables must be greater than 2
# 定义变量最大最小值
max_min <- data.frame(
Biology = c(20, 0), Physics = c(20, 0), Maths = c(20, 0),
Sport = c(20, 0), English = c(20, 0), Geography = c(20, 0),
Art = c(20, 0), Programming = c(20, 0), Music = c(20, 0)
)
rownames(max_min) <- c("Max", "Min")
# 合并数据
df <- rbind(max_min, exam_scores)
df
> df
Biology Physics Maths Sport English Geography Art Programming Music
Max 20.0 20 20.0 20.0 20.0 20.0 20.0 20 20
Min 0.0 0 0.0 0.0 0.0 0.0 0.0 0 0
Student.1 7.9 10 3.7 8.7 7.9 6.4 2.4 0 20
Student.2 3.9 20 11.5 20.0 7.2 10.5 0.2 0 20
Student.3 9.4 0 2.5 4.0 12.4 6.5 9.8 20 20
Basic Radar Chart
#以student 1为例
library(fmsb)
student1_data <- df[c("Max", "Min", "Student.1"), ]
radarchart(student1_data)
Advanced Radar Chart
parameter settings
Variable options
vlabels
: variable labelvlcex
: variable label font size
Polygon options:
pcol
: line colorpfcol
: fill colorplwd
: line widthplty
: line type. Use numbers 1-6 or character vectors ("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"). Useplty = 0
orplty = “blank”
to not display lines.
Grid options:
cglcol
: grid line colorcglty
: grid line typecglwd
: grid line width
Axis options:
axislabcol
: The color of the axis labels and numbers. The default setting is "blue".caxislabels
: character vector to use as labels on the central axis.
radarchart(
student1_data, axistype = 1,
# Customize the polygon
pcol = "#00AFBB", pfcol = scales::alpha("#00AFBB", 0.5), plwd = 2, plty = 1,
# Customize the grid
cglcol = "grey", cglty = 1, cglwd = 0.8,
# Customize the axis
axislabcol = "grey",
# Variable labels
vlcex = 0.7, vlabels = colnames(student1_data),
caxislabels = c(0, 5, 10, 15, 20))
Multiple sets of radar charts
radarchart(
df, axistype = 1,
# Customize the polygon
pcol = c("#00AFBB", "#E7B800", "#FC4E07"), pfcol = scales::alpha(c("#00AFBB", "#E7B800", "#FC4E07"),0.5), plwd = 2, plty = 1,
# Customize the grid
cglcol = "grey", cglty = 1, cglwd = 0.8,
# Customize the axis
axislabcol = "grey",
# Variable labels
vlcex = 0.7, vlabels = colnames(student1_data),
caxislabels = c(0, 5, 10, 15, 20))
# Add an horizontal legend
legend(
x = "bottom", legend = rownames(df[-c(1,2),]), horiz = TRUE,
bty = "n", pch = 20 , col = c("#00AFBB", "#E7B800", "#FC4E07"),
text.col = "black", cex = 1, pt.cex = 1.5
)
grade
devtools::install_github("ricardo-bion/ggradar")
library("ggradar")
data preparation
library(tidyverse)
# 将行名改为分组列
df <- exam_scores %>% rownames_to_column("group")
df
> df
group Biology Physics Maths Sport English Geography Art Programming Music
1 Student.1 7.9 10 3.7 8.7 7.9 6.4 2.4 0 20
2 Student.2 3.9 20 11.5 20.0 7.2 10.5 0.2 0 20
3 Student.3 9.4 0 2.5 4.0 12.4 6.5 9.8 20 20
Basic Radar Chart
ggradar(
df[1, ],
values.radar = c("0", "10", "20"),# 最小,平均和最大网格线显示的值
grid.min = 0, # 绘制最小网格线的值
grid.mid = 10, # 绘制平均网格线的值
grid.max = 20 # 绘制最大网格线的值
)
Advanced Radar Chart
ggradar(
df[1, ],
values.radar = c("0", "10", "20"),
grid.min = 0, grid.mid = 10, grid.max = 20,
# Polygons
group.line.width = 1,
group.point.size = 3,
group.colours = "#00AFBB",
# Background and grid lines
background.circle.colour = "white",
gridline.mid.colour = "grey"
)
Grouped Radar Chart
ggradar(
df,
values.radar = c("0", "10", "20"),
grid.min = 0, grid.mid = 10, grid.max = 20,
# Polygons
group.line.width = 1,
group.point.size = 3,
group.colours = c("#00AFBB", "#E7B800", "#FC4E07"),
# Background and grid lines
background.circle.colour = "white",
gridline.mid.colour = "grey",
legend.position = "bottom"
)
Reference
https://www.datanovia.com/en/blog/beautiful-radar-chart-in-r-using-fmsb-and-ggplot-packages/
Past
- END -