📜  如何在 R 中创建雷达图?

📅  最后修改于: 2022-05-13 01:55:06.017000             🧑  作者: Mango

如何在 R 中创建雷达图?

在本文中,我们将了解如何在 R 编程语言中创建雷达图。

雷达图也称为蜘蛛图或 Web 或极坐标图。它是一个图形图表,用于显示从同一点开始的轴上表示的三个或更多定量变量的二维图表中的多变量数据

正在使用的数据集:

fmsb 包是几个实用函数,它具有在 R 中创建雷达图的 radarchar() 方法。

要安装软件包:

install.packages( fmsb )

在 R 中创建基本雷达图

在这里,我们将使用radarchart() 方法显示基本的雷达图。为此,我们将传递创建的数据框。

示例:基本雷达图

R
data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
# The default radar chart
radarchart(data)


R
data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
radarchart(data, pcol = "Green")


R
data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
radarchart(data, pfcol = "Green")


R
data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
radarchart(data, plwd = 3)


R
data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
radarchart(data,
           cglcol="Blue", cglty=10, axislabcol="grey",
           caxislabels=seq(0,20,5), cglwd=0.8)


输出:

线条颜色定制

pcol属性用于改变多边形线的颜色。

示例:线条颜色自定义

R

data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
radarchart(data, pcol = "Green")

输出:

将颜色填充到多边形中

pfcol属性用于填充多边形的颜色。

示例:填充颜色

R

data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
radarchart(data, pfcol = "Green")

输出:

更改线宽

plwd属性用于增加或减少多边形的线宽。

示例:更改线宽

R

data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
radarchart(data, plwd = 3)

输出:

雷达图中的网格特征

以下属性用于网格:

  • cglcol 用于绘制网络的颜色
  • cglty 用于获取网线类型
  • axislabcol 用于轴标签的颜色
  • caxislabels 用于显示轴标签的向量
  • cglwd 是净宽度

示例:网格特征

R

data <- as.data.frame(matrix( sample( 2:20 , 10 ,
                                     replace=T) ,
                             ncol=10))
 
colnames(data) <- c("Mumbai" , "Tamil" , "Noida" ,
                    "Kerala" , "Patna", "Assam" ,
                    "Ranchi" , "Bhopal", "Delhi",
                    "Indore" )
  
data <- rbind(rep(39,10) , rep(0,10) , data)
 
# Library
library(fmsb)
 
radarchart(data,
           cglcol="Blue", cglty=10, axislabcol="grey",
           caxislabels=seq(0,20,5), cglwd=0.8)

输出: