【问题标题】:R ggplot: Percentage histogram with facet_wrapR ggplot:带有 facet_wrap 的百分比直方图
【发布时间】:2020-10-22 11:38:07
【问题描述】:

我有这个 df:

     ID values Pop1
1 PDAC1 611648 Nafr
2 PDAC1 322513 Nafr
3 PDAC2 381089 Nafr
4 PDAC2  16941 Nafr
5 PDAC3  21454 Jud
6 PDAC3 658802 Jud

我想在“Pop1”列上使用 facet_wrap 制作两个直方图:

ggplot(all.samples2) +
  aes(x = values, fill = Pop1, colour = Pop1, after_stat(density)) +
  geom_histogram(bins = 30L) +
  theme_minimal() +
  facet_wrap(vars(Pop1)) +
  theme_bw() +
  theme(aspect.ratio=1)

但我不想使用这些值,而是使用每个人口中的百分比。 所以例如对于Pop1 = Nafr,我的直方图将显示 bin 0 到 300000 中 25% 的数据,bin 300000 到 600000 中 50% 的数据以及 bin 600001 到 900000 中 25% 的数据。

我该怎么做?

谢谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用geom_bar 代替geom_histogram 并提供y = ..prop..

      [![ggplot(df) +
      aes(x = values, fill = Pop1, colour = Pop1) +
      theme_minimal() +
      facet_wrap(vars(Pop1)) +
      geom_bar(aes(y = ..prop..)) + 
      theme_bw() +
      theme(aspect.ratio = 1) + 
      labs(y = "") + 
      scale_y_continuous(labels = scales::percent)][1]][1]
    

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2015-04-13
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多