有没有办法为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
如何直接生成与以前相同的直方图(不重新创建以前的数据集)?
我目前有一个数据集,其中我有不同组的某些分类变量的频率,我希望绘制一个直方图,比较不同组中分类变量的频率。
答案 0 :(得分: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")