【问题标题】:How to plot Radar chart如何绘制雷达图
【发布时间】:2020-07-20 11:33:14
【问题描述】:

能否请您告诉我如何使用以下数据集进行雷达图。 在这个数据集中,我想按值查看 fut 列的分布 total 并按名为 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))

感谢您的帮助! 阿玛雷

【问题讨论】:

    标签: r ggplot2 radar-chart


    【解决方案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)
    

    添加:新数据集的示例: 对于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)
    

    【讨论】:

    • 谢谢,但是你能不能把两者都放在一个雷达图中,用不同的颜色表示“I”和“M”。谢谢!
    • 非常感谢。但是,我认为我为我的问题提供了错误的模拟数据。这个新的模拟数据适合我的问题。我可以通过计数的绝对值查看 da 跨组的分布情况。 ´´´数据
    • 我不确定雷达图是否最适合此类数据。无论如何,您需要将变量作为列和组作为行的 data.frame。因此,您需要为三个额外变量中的每一个绘制单独的图。我添加了一个示例。
    • 非常感谢亲爱的 Matushiq!我想我现在找到了解决方案。