R语言可视化(二十一):雷达图绘制

21. 雷达图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/21radar/")

使用fmsb包绘制雷达图

# 安装并加载所需的R包
#install.packages("fmsb")
library(fmsb)

# 构建数据集
# Data must be given as the data frame, where the first cases show maximum.
# 用于生成雷达图的最大最小值,第一行为最大值,第二行为最小值
maxmin <- data.frame(
  total=c(5, 1),
  phys=c(15, 3),
  psycho=c(3, 0),
  social=c(5, 1),
  env=c(5, 1))
head(maxmin)
##   total phys psycho social env
## 1     5   15      3      5   5
## 2     1    3      0      1   1

set.seed(123)
dat <- data.frame(
  total=runif(3, 1, 5),
  phys=rnorm(3, 10, 2),
  psycho=c(0.5, NA, 3),
  social=runif(3, 1, 5),
  env=c(5, 2.5, 4))
head(dat)
##      total      phys psycho   social env
## 1 2.150310 12.380413    0.5 2.826459 5.0
## 2 4.153221  6.620889     NA 4.827333 2.5
## 3 2.635908 12.478992    3.0 2.813337 4.0

# combine data
dat <- rbind(maxmin,dat)
head(dat)
##      total      phys psycho   social env
## 1 5.000000 15.000000    3.0 5.000000 5.0
## 2 1.000000  3.000000    0.0 1.000000 1.0
## 3 2.150310 12.380413    0.5 2.826459 5.0
## 4 4.153221  6.620889     NA 4.827333 2.5
## 5 2.635908 12.478992    3.0 2.813337 4.0

# 使用radarchart函数绘制雷达图
radarchart(dat, 
           axistype=1, #设定axes的类型,1 means center axis label only
           seg=5, #设定网格的数目
           plty=1, #设定point连线的线型
           vlabels=c("Total\nQOL", "Physical\naspects", 
                     "Phychological\naspects", "Social\naspects", 
                     "Environmental\naspects"), 
           title="(axis=1, 5 segments, with specified vlabels)", 
           vlcex=1 #设置标签的字体粗细大小
           )
image.png
radarchart(dat, 
           axistype=2, 
           pcol=topo.colors(3), 
           plty=1, pdensity=c(5, 10, 30), 
           pangle=c(10, 45, 120), 
           pfcol=topo.colors(3), 
           title="(topo.colors, fill, axis=2)")
image.png
radarchart(dat, 
           axistype=3, pty=16, plty=2, 
           axislabcol="grey", na.itp=FALSE,
           title="(no points, axis=3, na.itp=FALSE)")
image.png
radarchart(dat, 
           axistype=1, plwd=1:5, pcol=1, centerzero=TRUE, 
           seg=4, caxislabels=c("worst", "", "", "", "best"),
           title="(use lty and lwd but b/w, axis=1,\n centerzero=TRUE, with centerlabels)")
image.png

使用ggradar包绘制雷达图

# 安装并加载所需的R包
#devtools::install_github("ricardo-bion/ggradar", dependencies=TRUE)
library(ggradar)

# 构建示例数据
library(dplyr)
library(scales)
library(tibble)

mtcars_radar <- mtcars %>% 
  as_tibble(rownames = "group") %>% 
  mutate_at(vars(-group), rescale) %>% 
  tail(4) %>% 
  select(1:10)
# 查看示例数据
mtcars_radar
## # A tibble: 4 x 10
##   group            mpg   cyl  disp    hp  drat    wt   qsec    vs    am
##   <chr>          <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl>
## 1 Ford Pantera L 0.230   1   0.698 0.749 0.673 0.424 0          0     1
## 2 Ferrari Dino   0.396   0.5 0.184 0.435 0.396 0.321 0.119      0     1
## 3 Maserati Bora  0.196   1   0.573 1     0.359 0.526 0.0119     0     1
## 4 Volvo 142E     0.468   0   0.124 0.201 0.622 0.324 0.488      1     1

# 使用ggradar函数绘制雷达图
ggradar(mtcars_radar)
image.png
ggradar(mtcars_radar,base.size = 12,
        values.radar = c("0%","25%","50%","75%","100%"),
        legend.title = "group",legend.text.size = 12,
        legend.position = "right")
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] tibble_2.1.3 scales_1.0.0 dplyr_0.8.3  ggradar_0.2  fmsb_0.7.0  
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5       pillar_1.4.2     compiler_3.6.0   tools_3.6.0     
##  [5] digest_0.6.20    evaluate_0.14    gtable_0.3.0     pkgconfig_2.0.2 
##  [9] rlang_0.4.7      cli_1.1.0        yaml_2.2.0       xfun_0.8        
## [13] stringr_1.4.0    knitr_1.23       vctrs_0.3.2      grid_3.6.0      
## [17] tidyselect_0.2.5 glue_1.3.1       R6_2.4.0         fansi_0.4.0     
## [21] rmarkdown_1.13   ggplot2_3.2.0    purrr_0.3.2      magrittr_1.5    
## [25] htmltools_0.3.6  assertthat_0.2.1 colorspace_1.4-1 labeling_0.3    
## [29] utf8_1.1.4       stringi_1.4.3    lazyeval_0.2.2   munsell_0.5.0   
## [33] crayon_1.3.4

推荐阅读更多精彩内容