雷达图(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

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐