我有一个看起来像这样的数据文件......
Rate <- runif(14, 0, 20)
Day <- c("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday",
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday")
Grouper <- c(rep(1, 7), rep(2, 7))
df <- data.frame(Rate, Day, Grouper)
...我想制作一个每天有两个小节的条形图:Grouper = 1
的一个小节和Grouper = 2
的一个小节。 y值不是计数,它是Rate
变量,因此我需要使用stat = "identity"
来使其正常工作...
# Set max chart height
maxlimit = max(df$Rate) * 1.1
# Actual plot code
ggplot(df, aes(Day, Rate)) +
geom_bar(stat = "identity") +
geom_bar(aes(fill = Grouper), position = "dodge") +
scale_y_continuous(limits = c(0, maxlimit)) +
theme_classic()
...但我仍然收到错误stat_count() must not be used with a y aesthetic.
有人可以向我解释为什么我会收到此错误以及我可以采取哪些措施来解决此问题?
答案 0 :(得分:1)
我的错误是两次致电geom_bar
。我以为我应该这样做,但我错了;第二个调用只是重新设置geom_bar
设置,从而删除对stat=identity.
的调用此代码有效:
ggplot(TSdata, aes(Day, Rate, group = Grouper, col = Grouper)) +
geom_bar(stat = "identity", aes(fill = Grouper), position = "dodge") +
scale_y_continuous(limits = c(0, maxlimit)) +
theme_classic()