【问题标题】:geom_histogram (ggplot2): manually inputting valuesgeom_histogram (ggplot2): 手动输入值
【发布时间】:2015-10-22 23:38:11
【问题描述】:

有没有办法在 ggplot 中为分类变量输入直方图高度的自定义值?

给定这个示例数据集

> example
    category group
1:         a  blah
2:         b  blah
3:         a  blah
4:         c  blah
5:         a  blah
6:         b   ugh
7:         a   ugh
8:         c   ugh
9:         b   ugh
10:        a   ugh
11:        b   ugh

我可以生成以下直方图

使用以下代码

ggplot(example, aes(x = category, fill = group)) + geom_histogram(position = 'dodge')

但是,如果我有以下计数数据集怎么办?

   category blah.count ugh.count
1:        a          3         2
2:        b          1         3
3:        c          1         1

如何直接生成与以前相同的直方图(不重新创建以前的数据集)?

我目前有一个数据集,其中包含不同组的某些分类变量的频率,我希望绘制一个直方图来比较不同组中分类变量的频率。

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    您正在寻找stat = "identity"。我使用melt来绘制这两个变量,但可能还有另一种方法。

    category <- c("a", "b", "c")
    blah.count <- c(3, 1, 1)
    ugh.count <- c(2, 3, 1)
    df <- data.frame(category, blah.count, ugh.count)
    
    library(reshape2)
    library(ggplot2)
    
    df <- melt(df)
    ggplot(data = df, aes(y = value, x = category, fill = variable)) +   
      geom_bar(stat = "identity", position = "dodge")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      相关资源
      最近更新 更多