Interaction Plot in ggplot2

前端 未结 4 1597
情深已故
情深已故 2020-12-28 16:16

I\'m trying to make interaction plot with ggplot2. My code is below:

library(ggplot2)
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom         


        
4条回答
  •  别那么骄傲
    2020-12-28 16:39

    You can precalculate the values in their own data frame:

    toothInt <- ddply(ToothGrowth,.(dose,supp),summarise, val = mean(len))
    
    ggplot(ToothGrowth, aes(x = factor(dose), y = len, colour = supp)) + 
        geom_boxplot() + 
        geom_point(data = toothInt, aes(y = val)) +
        geom_line(data = toothInt, aes(y = val, group = supp)) + 
        theme_bw()
    

    enter image description here

    Note that using ggplot rather than qplot makes the graph construction a lot clearer for more complex plots like these (IMHO).

提交回复
热议问题