【问题标题】:How to create back to back histogram using ggplot2 in R? [duplicate]如何在 R 中使用 ggplot2 创建背靠背直方图? [复制]
【发布时间】:2021-01-15 03:35:31
【问题描述】:

我有一个名为TP_FPdf,并希望使用ggplot2 基于Group 列创建一个背靠背(镜像)直方图。

 `TP_FP`
           Value        Group        
            <dbl>       <chr>         
         1 -0.00540   False positive
         2  0.331     True positive
         3 -1.11      False positive
         4  1.4365    False positive
         5 -0.586     True positive
         6  1.26      True positive
         7  0.5463    False positive
         8  3.245     False positive
         9 -0.950     False positive
        10 10.4354    True positive

我尝试了以下方法:

ggplot(TP_FP, aes(x=x)) +
      geom_histogram(aes(x=Value, y = -..density.., fill= Group == "False positive"),col="black", binwidth = 0.1) +
      geom_histogram( aes(x=Value,y = ..density.., fill= Group =="True positive"),col="black", binwidth = 0.1) +
      labs(x="R Ratio", y="Number of proteins") +
      theme_bw() + theme(legend.position="right")

我希望得到一个像底部那样的合并直方图。

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    像这样?

    library(tidyverse)
    TP_FP <- tribble(~Value, ~Group,        
                    -0.00540, "False positive",
                    0.331, "True positive",
                    -1.11, "False positive",
                    1.4365, "False positive",
                    -0.586, "True positive",
                    1.26, "True positive",
                    0.5463, "False positive",
                    3.245, "False positive",
                    -0.950, "False positive",
                    10.4354, "True positive")
    
    ggplot() +
      geom_histogram(data = TP_FP %>% filter(Group == "False positive"),
                     aes(y = -(..density..), x = Value, fill = Group),
                     col = "black", binwidth = 0.1) +
      geom_histogram(data = TP_FP %>% filter(Group == "True positive"),
                     aes(y = ..density.., x = Value, fill = Group),
                     col = "black", binwidth = 0.1) +
      labs(x = "R Ratio", y = "Number of proteins") +
      theme_bw() + theme(legend.position = "right")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2010-11-23
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      相关资源
      最近更新 更多