I'm trying to make interaction plot with ggplot2
. My code is below:
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")
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = 1))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
How can I plot dose-supp level combination means rather than only dose level means which I'm getting here? Thanks in advance for your help.
a much easier way. without ddply. directly with ggplot2.
You can precalculate the values in their own data frame:
Note that using
ggplot
rather thanqplot
makes the graph construction a lot clearer for more complex plots like these (IMHO).You can compute your summaries by the appropriate groups (
supp
):Or converting to
ggplot
syntax (and combining into one expression)EDIT:
To make this work with 0.9.3, it effectively becomes Joran's answer.
If you think you might need a more general approach, you could try function rxnNorm in package HandyStuff (github.com/bryanhanson/HandyStuff). Disclaimer: I'm the author. Disclaimer #2: the box plot option doesn't quite work right, but all the other options are fine.
Here's an example using the ToothGrowth data: