如何绘制雷达图

时间:2020-07-20 11:33:14

标签: r ggplot2 radar-chart

请告诉我如何使用以下数据集绘制雷达图。 在此数据集中,我想通过值查看 fut 列的分布 总计,并通过名为 Group 的变量进行分类。 这是模拟数据集

maxmin <- data.frame(
      total=c(5, 1,5,7),
      phys=c(15, 3, 9,3),
      psycho=c(3, 0,3,5),
      social=c(5, 1,4,6),
      env=c(5, 1,5,2),
      fut = c("am", "ber", "am", "ber"),
      group = rep(c("I", "M"), each = 2))

感谢您的帮助! 阿玛雷

1 个答案:

答案 0 :(得分:1)

您可以使用fmsb软件包。

library(fmsb)
library(dplyr)
# Group names needs to be as row names of data.frame
x <- maxmin %>% 
  mutate(name = paste(fut, group, sep = "_")) %>%
  select(-c(fut, group))
df <- as.data.frame(x %>% select(-c(name)))
rownames(df) <- x$name
# max & min needs to be added as first and second row, respectively
df <- rbind(rep(20,5) , rep(0,5) , df)
par(mar=rep(0.8,4))
par(mfrow=c(1,2))
radarchart(df[c(1:2, 3:4), ], title = "I")
radarchart(df[c(1:2, 5:6), ], title = "M")

resulting graph

编辑: 全部合一:

## All in one
library(fmsb)
library(dplyr)
# Group names needs to be as row names of data.frame
x <- maxmin %>% 
  mutate(name = paste(fut, group, sep = "_")) %>%
  select(-c(fut, group))
df <- as.data.frame(x %>% select(-c(name)))
rownames(df) <- x$name
# max & min needs to be added as first and second row, respectively
df <- rbind(rep(20,5) , rep(0,5) , df)

radarchart(df,
           pcol = c("steelblue", "steelblue", "tomato", "tomato"), # colors define Group
           plty = c(1, 2, 1, 2), # line type defines fut
           plwd = 2) # line width
# Add a legend
legend(x=0.7, y=1, legend = rownames(df[-c(1,2),]), bty = "n", pch=20 , col = c("steelblue", "steelblue", "tomato", "tomato"), text.col = "grey", cex=1.1, pt.cex=2)

All in one

添加:带有新数据集的示例: 对于radarchart,您需要具有data.frame,其中变量在列中,而组在行中。因此,对于您的特定数据集data,您需要创建3个单独的图(FClog,pvalue,count)。 这是FClog的示例:

#Example with new dataset "data"
library(fmsb)
library(dplyr)
library(tidyr)
set.seed(100) # you may want to set this for reproducibility example
data <- data.frame(da = c("total", "phys","psycho","social","fut", "total","phys", "psycho","social","fut"), logFC = runif(10, -1, 1), pvalue = runif(10, max= 0.05, min= 0.001), count = runif(10,max = 20, min = 12), group = rep(c("WT","KO"), each = 5))

x1 <- data %>% select(da, logFC, group) %>%
  pivot_wider(names_from = group, values_from = logFC) %>%
  as.data.frame()
rownames(x1) <- x1[,1]
x1 <- x1[,2:ncol(x1)]
x1 <- x1 %>%
  t() %>%
  as.data.frame()

# max & min needs to be added as first and second row, respectively
df <- rbind(rep(1,ncol(x1)) , rep(-1,ncol(x1)) , x1)
par(mar=rep(0.8,4))
radarchart(df,
           title = "FClog",
           pcol = c("steelblue", "tomato"), # colors define Group
           plty = c(1, 2), # line type defines fut
           plwd = 2) # line width
# Add a legend
legend(x=0.7, y=1, legend = rownames(df[-c(1,2),]), bty = "n", pch=20 , col = c("steelblue", "tomato"), text.col = "grey", cex=1.1, pt.cex=2)

enter image description here