其他
R语言绘制雷达图的示例
(来源文献:Crop rotation of flooded rice with upland maize impacts the resident and active methanogenic microbial community)
接下来展示几种R语言绘制雷达图的方法示例。
fmsb包的雷达图
fmsb包是常用的绘制雷达图的包。
library(fmsb)#模拟数据
#先定义每个元素的取值范围
dat1 <- data.frame(
factor1 = c(5, 0),
factor2 = c(10, 0),
factor3 = c(8, 0),
factor4 = c(6, 0),
factor5 = c(9, 0))
#再将每个对象的各元素数值叠加在下方
set.seed(1234)
dat2 <- data.frame(
factor1 = runif(3, 1, 5),
factor2 = runif(3, 1, 10),
factor3 = runif(3, 1, 8),
factor4 = runif(3, 1, 6),
factor5 = runif(3, 1, 9))
dat <- rbind(dat1, dat2)
#查看数据集结构
dat
par(mar = c(1, 2, 2, 1),mfrow = c(2, 2))
radarchart(dat, axistype = 1, seg = 5, plty = 1, vlcex = 1)
radarchart(dat, axistype = 2, pcol = topo.colors(3), plty = 1, pdensity = c(5, 10, 30),
pangle = c(10, 45, 120), pfcol = topo.colors(3))
radarchart(dat, axistype = 3, pty = 32, plty = 1, axislabcol = 'grey', na.itp = FALSE)
radarchart(dat, axistype = 1, pcol = c('#4C00FF', '#00FF4D', '#FFFF00'), pfcol = c('#4C00FF55', '#00FF4D55', '#FFFF0055'),
centerzero = TRUE, seg = 4, caxislabels = c('worst', '', '', '', 'best'))
ggradar包的雷达图
ggradar是ggplot2的拓展包,调用ggplot2的语法绘制雷达图。如果平时习惯ggplot2作图,那么这个包使用起来可能会比fmsb包更顺手,因为它的参数选项的名称和ggplot2的很像。
#通过连接入 github 安装#install.packages('devtools')
devtools::install_github('ricardo-bion/ggradar', dependencies = TRUE)
library(ggradar)
#模拟数据
set.seed(1234)
dat <- data.frame(
obj = c('obj1', 'obj2', 'obj3'),
factor1 = runif(3, 0, 1),
factor2 = runif(3, 0, 1),
factor3 = runif(3, 0, 1),
factor4 = runif(3, 0, 1),
factor5 = runif(3, 0, 1))
#查看数据集结构
dat
ggradar(dat, background.circle.transparency = 0, group.colours = c('blue', 'red', 'green3'))
plotly包的雷达图
如果期望绘制交互式的雷达图,那么plotly包可满足需求。
#plotly 包的交互式雷达图的一个示例,详情 ?plot_lylibrary(plotly)
p <- plot_ly(
type = 'scatterpolar',
fill = 'toself'
) %>%
add_trace(
r = c(39, 28, 8, 7, 28, 39),
theta = c('A','B','C', 'D', 'E', 'A'),
name = 'Group A'
) %>%
add_trace(
r = c(1.5, 10, 39, 31, 15, 1.5),
theta = c('A','B','C', 'D', 'E', 'A'),
name = 'Group B'
) %>%
layout(
polar = list(
radialaxis = list(
visible = TRUE,
range = c(0,50)
)
)
)
p
其它
最后容俺摆一张图,触景生情,回忆那些年快乐但再也找不回的时光