我正在尝试生成以下数据的直方图(数据来自sqlserver数据库)
> head(Data)
value temp
1 47.34848 97
2 45.95588 97
3 47.34848 97
4 46.99248 97
5 46.64179 97
6 46.29630 97我在ggplot中尝试了qplot和dodging。我希望我能得到多个直方图,但我只得到了一个直方图

> qplot(value, data=Data, geom = "bar", fill = temp, position = "dodge")为了验证我在数据中有两个不同的温度,我生成了一个温度直方图
> qplot(temp,data=Data,geom="bar")

我还生成了该值的直方图,它与上面的第一个图相同。为了验证我的命令,我生成了一个带有一些样本数据的图,我使用的命令似乎是正确的
> head(SampleData)
val cat
1 1 a
2 2 a
3 3 a
4 4 a
5 4 a
6 2 a

请帮我找到问题所在
发布于 2012-02-22 14:13:46
用于定义这两个组的变量应为factor。
# Sample data
n <- 100
d <- sample( c(TRUE,FALSE), n, replace=TRUE )
d <- data.frame(
value = ifelse(d, 10, 30 ) + 10 * rnorm(n),
temp = ifelse(d,0,97)
)
# Make sure temp is a factor
p <- ggplot(d, aes(x=value, fill=factor(temp)))
p + geom_histogram(position="stack")
p + geom_histogram(position="dodge") https://stackoverflow.com/questions/9389666
复制相似问题