【问题标题】:Fill colors in geom_histogram(..density.., group = 1)在 geom_histogram(..density.., group = 1) 中填充颜色
【发布时间】:2013-09-29 19:55:23
【问题描述】:

geom_histogram 函数有一个基本问题

使用数据集:

df <- data.frame(value = factor( rep(c("A","B"), c(100,200) )))

我创建一个直方图:

ggplot(df, aes(x=value, fill = factor(value))) + geom_histogram()

输出是一个直方图,A 的计数为 100,B 的计数为 200

如果我改为绘制密度:

ggplot(df, aes(x=value, fill = factor(value), ..density..)) + geom_histogram()

输出是一个直方图,A 的密度为 1,B 的密度为 1。我认为原因是密度是分别在 A 和 B 上计算的。

创建的直方图:

ggplot(df, aes(x=value, group = 1, fill = factor(value),..density..)) + geom_histogram()

是一个直方图,其中 A 为 0.33,B 为 0.66,但填充颜色为黑色,在此版本的绘图中,我找不到获取先前直方图中使用的填充颜色的方法。

如何根据因子(值)生成具有填充颜色的最后一个版本的直方图?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我解决了这个问题:

    ggplot(df, aes(x=value, fill = factor(value))) + 
     geom_histogram(aes(y=..count../sum(..count..)))
    

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 2021-06-06
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2014-10-27
      • 2011-07-09
      相关资源
      最近更新 更多