【问题标题】:How to plot on a 100 percent y axis in R如何在 R 中绘制 100% 的 y 轴
【发布时间】:2016-09-28 00:11:48
【问题描述】:

我想使用 ggplot 绘制早上旅行期间雄性蝙蝠四种不同栖息地类型的百分比分布。

我的代码如下:

ggplot(subset(random_df_morn[which(random_df_morn$Sex =="male"),]), aes(x = daytime, fill=biotop_new, stat="bin"), main="morning")+ geom_histogram()

但在 y 轴上出现的是“计数”而不是“百分比”。

我补充的是:

+ geom_bar(aes(y = (..count..)/sum(..count..))) +  scale_y_continuous(labels=percent)

但由于“计数”是 600,现在百分比也限制在 60%。但我需要或我的目的是 100% 规模的分布。

如何将 y 轴设置为 100% 而不是 60%?

非常感谢您的帮助。

【问题讨论】:

  • 您能否提供一个您想要的图表类型的示例,以便澄清?无论如何,我认为这个问题的最佳答案 (stackoverflow.com/questions/21061653/…) 应该为您指明正确的方向。

标签: r ggplot2 percentage


【解决方案1】:

您的问题已部分回答here。您可以简单地在scale_y_continuous 函数中使用limits 参数。这是一个使用mtcars的可重现示例

library(ggplot2)
library(scales)
ggplot(mtcars, aes(x = factor(hp))) +  
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent, limits = c(0,1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多