绘图练习来源:
https://www.datanovia.com/en/blog/beautiful-radar-chart-in-r-using-fmsb-and-ggplot-packages/
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
## 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
注意: The data should be organized as follow:
The row 1 must contain the maximum values for each variable
The row 2 must contain the minimum values for each variable
Data for cases or individuals should be given starting from row 3
The number of columns or variables must be more than 2
library(fmsb)
# Define the variable ranges: maximum and minimum
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")
# Bind the variable ranges to the data
df <- rbind(max_min, exam_scores)
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
# Plot the data for student 1
library(fmsb)
student1_data <- df[c("Max", "Min", "Student.1"), ]
radarchart(student1_data)
# 通过定义函数
create_beautiful_radarchart <- function(data, color = "#00AFBB",
vlabels = colnames(data), vlcex = 0.7,
caxislabels = NULL, title = NULL, ...){
radarchart(
data, axistype = 1,
# Customize the polygon
pcol = color, pfcol = scales::alpha(color, 0.5), plwd = 2, plty = 1,
# Customize the grid
cglcol = "grey", cglty = 1, cglwd = 0.8,
# Customize the axis
axislabcol = "grey",
# Variable labels
vlcex = vlcex, vlabels = vlabels,
caxislabels = caxislabels, title = title, ...
)
}
op <- par(mar = c(1, 2, 2, 1))
create_beautiful_radarchart(student1_data, caxislabels = c(0, 5, 10, 15, 20))
par(op)
Create the radar chart of the three students on the same plot:
# Reduce plot margin using par()
op <- par(mar = c(1, 2, 2, 2))
# Create the radar charts
create_beautiful_radarchart(
data = df, caxislabels = c(0, 5, 10, 15, 20),
color = c("#00AFBB", "#E7B800", "#FC4E07")
)
# 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
)
par(op)
Create separated spider charts for each individual. This is recommended when you have more than 3 series.
# Define colors and titles
colors <- c("#00AFBB", "#E7B800", "#FC4E07")
titles <- c("Student.1", "Student.2", "Student.3")
# Reduce plot margin using par()
# Split the screen in 3 parts
op <- par(mar = c(1, 1, 1, 1))
par(mfrow = c(1,3))
# Create the radar chart
for(i in 1:3){
create_beautiful_radarchart(
data = df[c(1, 2, i+2), ], caxislabels = c(0, 5, 10, 15, 20),
color = colors[i], title = titles[i]
)
}
par(op)