繁体   English   中英

R ggplot:带有 facet_wrap 的百分比直方图

[英]R ggplot: Percentage histogram with facet_wrap

我有这个 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% 的数据。

我怎样才能做到这一点?

谢谢

您可以使用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]

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM