2016-06-30 67 views
0

我有一個數據文件看起來像這樣...[R ggplot:STAT =「身份」工作不因某種原因

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 = 1Grouper = 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.誰能給我解釋一下爲什麼我得到這個錯誤,我能做些什麼來解決它?

+3

你兩次調用GEOM,不更改默認的第二次。 – Roland

+0

啊,理解。以爲我遵循ggplot2文檔中設置的樣式,但我錯了。 – mmyoung77

回答

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()