【问题标题】:Reordering bar plots - R, ggplot, position = "dodge"重新排序条形图 - R, ggplot, position = "dodge"
【发布时间】:2017-04-20 18:23:04
【问题描述】:

我使用以下代码制作了一个数据框:

> p <- rep(c("5e-8", "0.05", "1"), 2)
> pgc1_rsq <- c(0.0037, 0.0726, 0.0847)
> meta_rsq <- c(0.0263, 0.1829, 0.1753)
> values <- c(pgc1_rsq, meta_rsq)
> Type <- c(rep("PGC1", 3), rep("PGC meta-analysis", 3))
> mydata <- data.frame(p, values)
> mydata$p <- factor(mydata$p, levels = c("5e-8", "0.05", "1"))

我使用以下代码创建了一个条形图:

> plot <-ggplot(mydata, aes(p, values))
> plot +geom_bar(stat = "identity", aes(fill = Type), position = "dodge") + xlab("P-value threshold") + ylab("Proportion of variance explained (Nagelkerke Rsq)")

产生了这个情节:

我现在想重新排序条形 - 以便在每对中“PGC1”位于“PGC 元分析”之前。我已经尝试使“值”和“类型”因素以及 PGC1 排在第一位的排序级别是这样的:

> mydata$value <- factor(mydata$value, levels = c("pgc1_rsq", "pgc_meta"))

但这给了我一个带有“类型”的错误消息,并且没有产生带有“值”的预期结果。

我们将不胜感激输入和建议。谢谢。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    为什么不在dataframe 中包含Type

    p <- rep(c("5e-8", "0.05", "1"), 2)
    pgc1_rsq <- c(0.0037, 0.0726, 0.0847)
    meta_rsq <- c(0.0263, 0.1829, 0.1753)
    values <- c(pgc1_rsq, meta_rsq)
    Type <- c(rep("PGC1", 3), rep("PGC meta-analysis", 3))
    mydata <- data.frame(p, values,Type)
    mydata$Type <- factor(mydata$Type, levels = c("PGC1","PGC meta-analysis"))
    mydata$p <- factor(mydata$p, levels = c("5e-8", "0.05", "1"))
    
    plot <-ggplot(mydata, aes(p, values))
    plot +geom_bar(stat = "identity", aes(fill = Type), position = "dodge") + xlab("P-value threshold") + ylab("Proportion of variance explained (Nagelkerke Rsq)")
    

    【讨论】:

      【解决方案2】:

      尝试在 geom_bar 中分解填充:

      plot <- ggplot(mydata, aes(p, values))
      plot + geom_bar(stat = "identity", aes(fill = factor(Type, levels = c("PGC1", "PGC meta-analysis"))), position = "dodge") + 
      xlab("P-value threshold") + 
      ylab("Proportion of variance explained (Nagelkerke Rsq)") 
      

      【讨论】:

        【解决方案3】:

        稍微清理一下你的数据框

        mydata <- data.frame(p = factor(rep(c("5e-8", "0.05", "1"), 2), levels = c("5e-8", "0.05", "1")), 
                         values =c(0.0037, 0.0726, 0.0847, 0.0263, 0.1829, 0.1753), 
                         Type = factor(c( rep("PGC meta-analysis", 3),rep("PGC1", 3)), levels = c("PGC1", "PGC meta-analysis")))
        
        
        
        ggplot(mydata, aes(p, values))+
          geom_bar(stat = "identity", aes(fill = Type), position = "dodge") + 
          xlab("P-value threshold") + 
          ylab("Proportion of variance explained (Nagelkerke Rsq)")
        

        【讨论】:

          【解决方案4】:

          这些都是很好的答案。最优雅的方法是使用来自tidyr 的非常有用的gather。大多数 ggplots 必须的

          library(dplyr)
          library(tidyr)
          library(ggplot2)
          
          data.frame(p = c("5e-8", "0.05", "1"),
                     `pgc1_rsq` = c(0.0037, 0.0726, 0.0847),                    # change names here
                     `pgc_meta` = c(0.0263, 0.1829, 0.1753)) %>% 
            gather(Type, Value, 2:3) %>% 
            mutate(Type = factor(Type, levels = c("pgc1_rsq", "pgc_meta"))) %>% # and here if you need
            ggplot(aes(x = p, y = Value, fill = Type)) +
            geom_bar(stat = "identity", position = "dodge") +
            xlab("P-value threshold") + 
            ylab("Proportion of variance explained (Nagelkerke Rsq)")
          

          【讨论】:

          • 可能值得介绍tidyverse,因为其中三个包正在使用中。
          • 我不喜欢阻塞我的命名空间,尤其是在这样的学习环境中,但我不想阻塞自己的命名空间。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-19
          • 1970-01-01
          • 2015-05-19
          • 2013-06-02
          • 2016-09-25
          • 1970-01-01
          相关资源
          最近更新 更多