导语
GUIDE ╲
雷达图是以从同一点开始的轴上表示的三个或更多个定量变量的二维图表的形式显示多变量数据的图形方法。
背景介绍
雷达图(又叫蜘蛛网图)。传统的雷达图将多个维度的数据映射到坐标轴上,这些坐标轴起始于同一个圆心点,结束于圆周边缘,将同一组的点使用线连接起来就成为雷达图,相当于平行坐标图,轴径向排列。
今天小编给大家介绍最经常用来绘制雷达图的两个工具包:FMSB和ggradar。FMSB是一个专门用来绘制雷达图的R包,其函数组成较为简单,应用也较为广泛。ggradar作为ggplot2的扩展包,非常适合习惯使用ggplot2绘图的小伙伴!
R包安装及函数介绍
##ggradar是一个来源于github的包
library(devtools)
install_github("ricardo-bion/ggradar")
library(ggradar)
##fmsb
install.packages("fmsb")
library(fmsb)
#函数介绍
##ggradar介绍
ggradar(plot.data, ##数据集,每一行是一组
##字体大小
base.size = 15,
font.radar = "sans",##文体
##小中大三个网格线
values.radar = c("0%", "50%", "100%"),
##轴标签名称
axis.labels = colnames(plot.data)[-1],
##三个网格线数值
grid.min = 0,grid.mid = 0.5, grid.max = 1,
##y在图中心的值
centre.y = grid.min - ((1/9) * (grid.max - grid.min)),
##图的相对大小(水平,垂直
plot.extent.x.sf = 1, plot.extent.y.sf = 1.2,)
label.centre.y = FALSE,
##网格线宽度,线型,颜色
grid.line.width = 0.5,gridline.min.linetype = "longdash",gridline.mid.linetype = "longdash",gridline.max.linetype = "longdash", gridline.min.colour = "grey",
gridline.mid.colour = "#007A87", gridline.max.colour = "grey",
##标签大小
grid.label.size = 6,
.....)
##fmsb
radarchart(df, ##数据集
axistype,##轴的类型(0-5代表5种格式)
seg,##每个轴的段数
pcol, #颜色代码1-8
plty, #线型设置1-6
plwd,#线宽设置
cglty, cglwd, cglcol, #雷达网络线型,线宽,颜色
axislabcol, title, ##轴标签和标题
...)
可视化介绍
01
FMSB绘制雷达图
1.1 数据展示和数据处理
#样本数据
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
#定义变量范围
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
1.2 首先画一个单样本默认参数的雷达图
# 对student 1绘制雷达图
student1_data <- df[c("Max", "Min", "Student.1"), ]
radarchart(student1_data)
在一开始小编给大家介绍了radarchart函数的诸多绘图参数,我们可以利用这些参数自定义一个绘图函数:create_beautiful_radarchart
create_beautiful_radarchart <- function(data, color = "#00AFBB",
vlabels = colnames(data), vlcex = 0.7,
caxislabels = NULL, title = NULL, ...){
radarchart(
data, axistype = 1,
# 自定义多边形
pcol = color, pfcol = scales::alpha(color, 0.5), plwd = 2, plty = 1,
# 网格设置
cglcol = "grey", cglty = 1, cglwd = 0.8,
# 轴颜色
axislabcol = "grey",
# 标签设置
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)
1.3 绘制多个样本的雷达图
op <- par(mar = c(1, 2, 2, 2))
# 绘制三个学生数据的雷达图
create_beautiful_radarchart(
data = df, caxislabels = c(0, 5, 10, 15, 20),
color = c("#00AFBB", "#E7B800", "#FC4E07")
)
# 添加图例
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)
如果我们有更多的样本数据时,绘制一个雷达图就会显得有些拥挤了,这个时候就可以为每个样本建立独立的雷达图。(依然以三个学生为例)
# 定义颜色和题目
colors <- c("#00AFBB", "#E7B800", "#FC4E07")
titles <- c("Student.1", "Student.2", "Student.3")
# 分面设置
op <- par(mar = c(1, 1, 1, 1))
par(mfrow = c(1,3))
#写个循环绘图
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)
雷达图在对样本进行比较时也十分好用(将学生2,3成绩与1比较)
opar <- par()
par(mar = rep(0.8,4))
par(mfrow = c(1,4))
for (i in 3:nrow(df)) {
radarchart(
df[c(1:3, i), ],
pfcol = c("#99999980",NA),
pcol= c(NA,2), plty = 1, plwd = 2,
title = row.names(df)[i]
)
}
# Restore the standard par() settings
par <- par(opar)
02
使用ggradar绘制雷达图
1.1 数据展示:
df <- exam_scores %>% rownames_to_column("group")
df
1.2 绘制基础的雷达图:
首先对student 1绘制基本雷达图:
ggradar(
df[1, ],
values.radar = c("0", "10", "20"),
grid.min = 0, grid.mid = 10, grid.max = 20
)
设置参数,对图形进行美化:
ggradar(
df[1, ],
values.radar = c("0", "10", "20"),
grid.min = 0, grid.mid = 10, grid.max = 20,
group.line.width = 1,
group.point.size = 3,
group.colours = "#00AFBB",
# 背景和网格线
background.circle.colour = "white",
gridline.mid.colour = "grey"
)
1.3 绘制多个样本的雷达图
ggradar(
df,
values.radar = c("0", "10", "20"),
grid.min = 0, grid.mid = 10, grid.max = 20,
group.line.width = 1,
group.point.size = 3,
group.colours = c("#00AFBB", "#E7B800", "#FC4E07"),
background.circle.colour = "white",
gridline.mid.colour = "grey",
legend.position = "bottom"
)
如果我们想统计更多人或样本有更多比例时,可以使用平行坐标图去替代雷达图:
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")
小编总结
雷达图是我们在统计数据时十分常用的一类图形,在本文中小编给大家介绍了两个绘制雷达图的R包:FMSB和ggradar,它们虽然参数和绘图风格都有差异,但都是使用起来非常方便的工具!希望大家能够开发它们的更多功能,绘制更漂亮的图片!