2012-12-08 50 views
1

該代碼不能按預期的方式與ggplot2 0.9.3一起工作(可與早期版本的ggplot2一起使用,請參閱here)。如果您建議解決此問題,我將非常感激。由於ggplot2的異常行爲0.9.3

library(ggplot2) 
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw() 
p <- p + labs(x="Dose", y="Response") 
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue", aes(group=supp)) 
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp)) 
p <- p + theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0)) 
p <- p + theme(axis.title.y = element_text(size = 12, angle = 90, vjust = 0.25)) 
print(p) 

編輯 此行

p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp)) 

產生以下警告

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic? 
+1

我認爲你應該打印一些中間步驟並說出問題是什麼? –

+0

@DWin:請參閱我的編輯。感謝您對我的問題表現出興趣。 – MYaseen208

+0

你能解釋一下警告中不清楚的地方嗎?每個組只包含一個值,並且您正在繪製該值的平均值,這沒有意義。 – Roland

回答

2

此行爲是在GGPLOT2 0.9.3中的錯誤:https://github.com/hadley/ggplot2/issues/739

你可以解決它通過使用ddply計算總結:

library(plyr) 
tg <- ddply(ToothGrowth, c("dose", "supp"), summarise, len = mean(len)) 

library(ggplot2) 
ggplot(ToothGrowth, aes(x=as.factor(dose), y=len, colour=supp)) + 
    geom_boxplot() + 
    geom_line(data=tg, aes(group=supp))