How to add a line in boxplot?

后端 未结 5 955
日久生厌
日久生厌 2021-02-04 09:14

I would like to add lines between \"mean\" in my boxplot.

My code:

library(ggplot2)
library(ggthemes)

Gp=factor(c(rep(\"G1\",80),rep(\"G2\",80)))
Fc=fac         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 09:50

    I have a way of doing this, surely similar to whats have been done, but using geom_line and position_dodge and data.table

    library(data.table)
    DATA=data.table(Y,X,Z,Fc,Gp)
    
     qplot(X, Y, data=DATA, geom="boxplot", fill=Z, na.rm = TRUE, 
               outlier.size = NA, outlier.colour = NA)  +
       geom_line(data = DATA[,list(Y = mean(Y)), by = .(X,Z,Fc,Gp)][X == "B1"],aes(X,Y,color = Z),group =1, position = position_dodge(width = .75),color = "black") +
       geom_line(data = DATA[,list(Y = mean(Y)), by = .(X,Z,Fc,Gp)][X == "B2"],aes(X,Y,color = Z),group =1, position = position_dodge(width = .75),color = "black") +
      facet_grid(Gp ~ Fc)+ theme_light()+
      theme(legend.position="bottom") +
      stat_summary(fun.y=mean, geom="point", shape=23, position = position_dodge(width = .75))
    

提交回复
热议问题