R combat | Radar Chart

R combat | Radar Chart

A radar chart , also known as a spider plot , is a chart that shows the strength of multidimensional data. It maps the amount of data in multiple dimensions to coordinate axes. These coordinate axes start from the same center point and usually end at the edge of the circle. Connecting the same group of points with a line is called a radar chart.

This article takes the R package fmsband ggradaras an example to introduce the drawing of radar charts.

fmsb

install.packages("fmsb")
library(fmsb)
# 示例数据
exam_scores <- data.frame(
    row.names = c("Student.1", "Student.2", "Student.3"),
      Biology = c(7.9, 3.9, 9.4),
      Physics = c(10, 20, 0),
        Maths = c(3.7, 11.5, 2.5),
        Sport = c(8.7, 20, 4),
      English = c(7.9, 7.2, 12.4),
    Geography = c(6.4, 10.5, 6.5),
          Art = c(2.4, 0.2, 9.8),
  Programming = c(0, 0, 20),
        Music = c(20, 20, 20)
)
exam_scores
> exam_scores
          Biology Physics Maths Sport English Geography Art Programming Music
Student.1     7.9      10   3.7   8.7     7.9       6.4 2.4           0    20
Student.2     3.9      20  11.5  20.0     7.2      10.5 0.2           0    20
Student.3     9.4       0   2.5   4.0    12.4       6.5 9.8          20    20

data preparation

Data requirements:

  • Row 1 must contain the maximum value of each variable

  • Line 2 must contain the minimum value of each variable

  • The number of columns or variables must be greater than 2

# 定义变量最大最小值
max_min <- data.frame(
  Biology = c(20, 0), Physics = c(20, 0), Maths = c(20, 0),
  Sport = c(20, 0), English = c(20, 0), Geography = c(20, 0),
  Art = c(20, 0), Programming = c(20, 0), Music = c(20, 0)
)
rownames(max_min) <- c("Max", "Min")

# 合并数据
df <- rbind(max_min, exam_scores)
df
> df
          Biology Physics Maths Sport English Geography  Art Programming Music
Max          20.0      20  20.0  20.0    20.0      20.0 20.0          20    20
Min           0.0       0   0.0   0.0     0.0       0.0  0.0           0     0
Student.1     7.9      10   3.7   8.7     7.9       6.4  2.4           0    20
Student.2     3.9      20  11.5  20.0     7.2      10.5  0.2           0    20
Student.3     9.4       0   2.5   4.0    12.4       6.5  9.8          20    20

Basic Radar Chart

#以student 1为例
library(fmsb)
student1_data <- df[c("Max", "Min", "Student.1"), ]
radarchart(student1_data)
ec9e02e75de5e0f89a438720e94fcc5c.png

Advanced Radar Chart

parameter settings

  • Variable options

    • vlabels: variable label

    • vlcex: variable label font size

  • Polygon options:

    • pcol: line color

    • pfcol: fill color

    • plwd: line width

    • plty: line type. Use numbers 1-6 or character vectors ("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"). Use plty = 0or plty = “blank”to not display lines.

  • Grid options:

    • cglcol: grid line color

    • cglty: grid line type

    • cglwd: grid line width

  • Axis options:

    • axislabcol: The color of the axis labels and numbers. The default setting is "blue".

    • caxislabels: character vector to use as labels on the central axis.

radarchart(
  student1_data, axistype = 1,
  # Customize the polygon
  pcol = "#00AFBB", pfcol = scales::alpha("#00AFBB", 0.5), plwd = 2, plty = 1,
  # Customize the grid
  cglcol = "grey", cglty = 1, cglwd = 0.8,
  # Customize the axis
  axislabcol = "grey", 
  # Variable labels
  vlcex = 0.7, vlabels = colnames(student1_data),
  caxislabels = c(0, 5, 10, 15, 20))
607ab76a077b62d9ef9ea056d3fe855c.png

Multiple sets of radar charts

radarchart(
  df, axistype = 1,
  # Customize the polygon
  pcol = c("#00AFBB", "#E7B800", "#FC4E07"), pfcol = scales::alpha(c("#00AFBB", "#E7B800", "#FC4E07"),0.5), plwd = 2, plty = 1,
  # Customize the grid
  cglcol = "grey", cglty = 1, cglwd = 0.8,
  # Customize the axis
  axislabcol = "grey", 
  # Variable labels
  vlcex = 0.7, vlabels = colnames(student1_data),
  caxislabels = c(0, 5, 10, 15, 20))
# Add an horizontal legend
legend(
  x = "bottom", legend = rownames(df[-c(1,2),]), horiz = TRUE,
  bty = "n", pch = 20 , col = c("#00AFBB", "#E7B800", "#FC4E07"),
  text.col = "black", cex = 1, pt.cex = 1.5
)
2eac8525422bc4e9e694608a45e8da3a.png

grade

devtools::install_github("ricardo-bion/ggradar")
library("ggradar")

data preparation

library(tidyverse)
# 将行名改为分组列
df <- exam_scores %>% rownames_to_column("group")
df
> df
      group Biology Physics Maths Sport English Geography Art Programming Music
1 Student.1     7.9      10   3.7   8.7     7.9       6.4 2.4           0    20
2 Student.2     3.9      20  11.5  20.0     7.2      10.5 0.2           0    20
3 Student.3     9.4       0   2.5   4.0    12.4       6.5 9.8          20    20

Basic Radar Chart

ggradar(
  df[1, ], 
  values.radar = c("0", "10", "20"),# 最小,平均和最大网格线显示的值
  grid.min = 0, # 绘制最小网格线的值
  grid.mid = 10, # 绘制平均网格线的值
  grid.max = 20 # 绘制最大网格线的值
)
43940fa3578920370dc1050ba0d15be4.png

Advanced Radar Chart

ggradar(
  df[1, ], 
  values.radar = c("0", "10", "20"),
  grid.min = 0, grid.mid = 10, grid.max = 20,
  # Polygons
  group.line.width = 1, 
  group.point.size = 3,
  group.colours = "#00AFBB",
  # Background and grid lines
  background.circle.colour = "white",
  gridline.mid.colour = "grey"
)
74f71c61ce6dc575d6b73d3de45ed523.png

Grouped Radar Chart

ggradar(
  df, 
  values.radar = c("0", "10", "20"),
  grid.min = 0, grid.mid = 10, grid.max = 20,
  # Polygons
  group.line.width = 1, 
  group.point.size = 3,
  group.colours = c("#00AFBB", "#E7B800", "#FC4E07"),
  # Background and grid lines
  background.circle.colour = "white",
  gridline.mid.colour = "grey",
  legend.position = "bottom"
  )
fefc7c4c085fdb355393fdb7ae7b8394.png

Reference

  • https://www.datanovia.com/en/blog/beautiful-radar-chart-in-r-using-fmsb-and-ggplot-packages/

Past

  1. Mapping with Cell Learning | Proteomaps Diagram

  2. R combat | ridgeline plot

  3. GOplot | More beautiful visualization of enrichment analysis

- END -eea411750b22da5b4fad674b9b365884.png

Guess you like

Origin blog.csdn.net/weixin_45822007/article/details/122795183
Recommended