文章目录
- 前言
- fmsb
- 数据格式
- 参数调整
- demo
- ggradar
- 单样本demo
- 多组demo
- 实战-给radar添加统计量
前言
雷达图,Radar
(也称为蜘蛛图
或网络图
)是一种二维图表,用于显示多变量数据。每个变量由从中心延伸出来的辐条(或轴)表示,数据点沿着这些辐条绘制。然后将数据点连接形成一个多边形,通过这种方式可以直观地展示多个变量之间的相对强弱。
- DOI: 10.1038/ncomms15107
在R语言里,作雷达图的主要用fmsb
和ggradar
这两个R包,前者一般绘制的是多边形雷达图,后者默认绘制的是圆形雷达图。大同小异,大家各自体会~
下面是两个R包的部分演示以及雷达图计算统计量的项目代码分享
下方公众号回复Pradar即可领取项目代码及文件
fmsb
数据格式
有2点需要注意:
- 作图数据必须要有一行最大值和一行最小值,用于雷达图边界划分。
- 列数必须大于2。
参数调整
- vlabels:变量标签
- vlcex:控制变量标签的字体大小
- pcol:线条颜色
- pfcol: 填色
- plwd: 行宽
- plty:线型。可以是数值向量 1:6 或字符向量 c(“solid”、“dashed”、“dotted”、“dotdash”、“longdash”、“twodash”)。要删除线,请使用plty = 0或plty = “blank”。
- cglcol:线条颜色
- cglty:线型
- cglwd: 行宽
- axislabcol:轴标签和数字的颜色。默认为“蓝色”。
- caxislabels:用作中心轴上的标签的字符向量。
demo
# Plotting student 1
ggradar(
df[1, ],
values.radar = c("0", "10", "20"),
grid.min = 0, grid.mid = 10, grid.max = 20
)
# 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)
# 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)
opar <- par()
# Define settings for plotting in a 3x4 grid, with appropriate margins:
par(mar = rep(0.8,4))
par(mfrow = c(3,4))
# Produce a radar-chart for each student
for (i in 4:nrow(df_scaled2)) {
radarchart(
df_scaled2[c(1:3, i), ],
pfcol = c("#99999980",NA),
pcol= c(NA,2), plty = 1, plwd = 2,
title = row.names(df_scaled2)[i]
)
}
# Restore the standard par() settings
par <- par(opar)
ggradar
gg系列一贯作风。雷达图大同小异,且直接用表达数据即可,无须另外计算界限值。太过于简单就不展示了
- plot.data:每个个人或团体占一行的数据
- values.radar:在最小、平均和最大网格线上显示的值
- grid.min:绘制最小网格线的值
- grid.mid:绘制平均网格线的值
- grid.max:绘制最大网格线的值
放上教程链接大家跟着来即可:https://www.datanovia.com/en/blog/beautiful-radar-chart-in-r-using-fmsb-and-ggplot-packages/#fmsb-radar-chart
此外还有棒棒糖图:
单样本demo
df2 <- t(exam_scores) %>%
as.data.frame() %>%
rownames_to_column("Field")
df2
ggdotchart(
df2, x = "Field", y = "Student.1",
add = "segments", sorting = "descending",
ylab = "Exam Score", title = "Student 1"
)
多组demo
df3 <- df2 %>%
select(Field, Student.1, Student.2) %>%
pivot_longer(
cols = c(Student.1, Student.2),
names_to = "student",
values_to = "value"
)
head(df3)
ggdotchart(
df3, x = "Field", y = "value",
group = "student", color = "student", palette = "jco",
add = "segment", position = position_dodge(0.3),
sorting = "descending"
)
df4 <- df2 %>%
select(Field, Student.1, Student.2, Student.3) %>%
pivot_longer(
cols = c(Student.1, Student.2, Student.3),
names_to = "student",
values_to = "value"
)
head(df4)
ggdotchart(
df4, x = "Field", y = "value",
group = "student", color = "student", palette = "jco",
add = "segment", position = position_dodge(0.3),
sorting = "descending", facet.by = "student",
rotate = TRUE, legend = "none"
)
library(GGally)
ggparcoord(
iris,
columns = 1:4, groupColumn = 5, order = "anyClass",
showPoints = TRUE,
title = "Parallel Coordinate Plot for the Iris Data",
alphaLines = 0.3
) +
theme_bw() +
theme(legend.position = "top")
实战-给radar添加统计量
这里随便找了个cibersort免疫浸润结果做的,此外还可以换成GSVA基因集评分结果、多基因组间表达等,也一样可以用雷达图。
用fmsb包,输入数据格式是一样的,第一二行为最大值和最小值:
由于计算p值及整合代码篇幅过长,已打包,文末领取~
pdf("radarplot.pdf", width = 9, height = 9)
# par(mar = c(1, 1, 1, 1))
par(family = "sans", cex = 1)
# 绘制雷达图并设置字体颜色
radarchart(data_radar, axistype = 1,
pcol = colors_border, pfcol = colors_in, plwd = 2,
cglcol = "grey", cglty = 1, axislabcol = "grey",
caxislabels = format(round(seq(0, max(as.numeric(as.character(unlist(data_radar)))), length.out = 5), 4), nsmall = 2),
cglwd = 0.8, vlcex = 0.8)
# 添加分组图例
legend(x = 0.9, y = 1.2, legend = rownames(data_radar)[c(3:4)], bty = "n", pch = 16, col = colors_border, text.col = "black", cex = 0.8, pt.cex = 1.5)
dev.off()
下方公众号回复Pradar即可领取项目代码及文件