MYaseen208
MYaseen208

Reputation: 23938

Unusual behavior of ggplot2 0.9.3

This code does not work as expected with ggplot2 0.9.3 (worked fine with earlier versions of ggplot2, See here). Is there a work-around for this problem?

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)

Edit

This line

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

produces the following warning

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

Upvotes: 2

Views: 1404

Answers (1)

wch
wch

Reputation: 4127

This behavior is a bug in ggplot2 0.9.3: https://github.com/hadley/ggplot2/issues/739

You can work around it by calculating the summaries using 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))

Upvotes: 2

Related Questions