R绘制雷达图

 雷达图(Radar Chart),又可称为戴布拉图、蜘蛛网图(Spider Chart),每个分类都拥有自己的数值坐标轴,这些坐标轴由中心向外辐射, 并用折线将同一系列的值连接。用以显示独立的数据系列之间,以及某个特定的系列与其他系列的整体之间的关系。

优点:适合展现某个数据集的多个关键特征并进行比对,适合比较多条数据在多个维度上的取值。

缺点:不适合展示多类别数据。

单组雷达图

构建示例数据集为例

# Library
library(fmsb)
 
# 构建数据集
data <- as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
 
# 新增两行最大值及最小值
data <- rbind(rep(20,10) , rep(0,10) , data)
 
# 绘制雷达图
radarchart(data)

个性化设置

  • 多边形特征:
    • pcol → 线条颜色
    • pfcol → 填充颜色
    • plwd → 线条宽度
       
  • 网格特征:
    • cglcol → 网络颜色
    • cglty → 网格线型
    • axislabcol →坐标轴标签颜色
    • caxislabels → 要显示的轴标签的向量
    • cglwd → 宽度
       
  • 标签:
    • vlcex → 组标签大小
# Library
library(fmsb)
 
# Create data: note in High school for Jonathan:
data <- as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
 
# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
data <- rbind(rep(20,10) , rep(0,10) , data)
 
# Check your data, it has to look like this!
# head(data)

# Custom the radarChart !
radarchart( data  , axistype=1 , 
 
    #custom polygon
    pcol=rgb(0.2,0.5,0.5,0.9) , pfcol=rgb(0.2,0.5,0.5,0.5) , plwd=4 , 
 
    #custom the grid
    cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
 
    #custom labels
    vlcex=0.8 
    )

多组雷达图 

# Library
library(fmsb)
 
# Create data: note in High school for several students
set.seed(99)
data <- as.data.frame(matrix( sample( 0:20 , 15 , replace=F) , ncol=5))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding" )
rownames(data) <- paste("mister" , letters[1:3] , sep="-")
 
# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each variable to show on the plot!
data <- rbind(rep(20,5) , rep(0,5) , data)
 
# plot with default options:
radarchart(data)

The radarchart() function offers several options to customize the chart:

  • Polygon features:
    • pcol → line color
    • pfcol → fill color
    • plwd → line width
       
  • Grid features:
    • cglcol → color of the net
    • cglty → net line type (see possibilities)
    • axislabcol → color of axis labels
    • caxislabels → vector of axis labels to display
    • cglwd → net width
       
  • Labels:
    • vlcex → group labels size

 改变坐标轴刻度

# Library
library(fmsb)
 
# Create data: note in High school for several students
set.seed(99)
data <- as.data.frame(matrix( sample( 0:20 , 15 , replace=F) , ncol=5))
colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding" )
rownames(data) <- paste("mister" , letters[1:3] , sep="-")
 
# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each variable to show on the plot!
data <- rbind(rep(20,5) , rep(0,5) , data)
 
# Set graphic colors
library(RColorBrewer)
coul <- brewer.pal(3, "BuPu")
colors_border <- coul
library(scales)
colors_in <- alpha(coul,0.3)

# If you remove the 2 first lines, the function compute the max and min of each variable with the available data:
radarchart( data[-c(1,2),]  , axistype=0 , maxmin=F,
    #custom polygon
    pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
    #custom the grid
    cglcol="grey", cglty=1, axislabcol="black", cglwd=0.8, 
    #custom labels
    vlcex=0.8 
    )

# Add a legend
legend(x=0.7, y=1, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)

 

Ref:

https://help.fanruan.com/finebi/doc-view-204.html

https://www.r-graph-gallery.com/142-basic-radar-chart.html

  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
R语言绘制雷达图,你可以使用fmsb包来实现。首先,你需要安装和加载fmsb包。请按照以下步骤进行操作: 1. 安装fmsb包: ```R install.packages("fmsb") ``` 2. 加载fmsb包: ```R library(fmsb) ``` 接下来,你需要准备数据来绘制雷达图雷达图适用于比较多个变量的值,并将其表示为一个多边形。每个变量的值在雷达图中都表示为一个“蛛网”的顶点。 下面是一个简单的例子,展示如何使用fmsb包绘制雷达图: ```R # 创建一个数据框,包含各个变量的值 data <- data.frame( Variable = c("Var1", "Var2", "Var3", "Var4", "Var5"), Value = c(80, 60, 70, 90, 75) ) # 将数据归一化,使得每个变量的值在0到1之间 data$Value <- data$Value / max(data$Value) # 创建一个空的雷达图 radarchart(rownames(data), matrix(data$Value), axistype = 1, seg = 5, plty = 1, caxislabels = seq(0, 1, 0.2), title = "Radar Chart Example") # 添加雷达图的数据点 points(data$Value * cos(pi * seq(0, 2, length.out = 5)), data$Value * sin(pi * seq(0, 2, length.out = 5)), col = "red", pch = 19, cex = 1.5) # 添加雷达图的连线 lines(data$Value * cos(pi * seq(0, 2, length.out = 5)), data$Value * sin(pi * seq(0, 2, length.out = 5)), col = "red", lwd = 2) ``` 这个例子中,我们使用了一个包含5个变量的数据框,并通过归一化将每个变量的值转换为0到1之间的范围。然后,我们使用radarchart函数创建了一个空的雷达图,并使用points和lines函数添加了数据点和连线。 你可以根据自己的数据和需求调整代码中的变量和数值。希望这个例子能够帮助你绘制出所需的雷达图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值