How to add a line in boxplot?

后端 未结 5 944
日久生厌
日久生厌 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:58

    This is not elegant but try this

    tmp1 = aggregate(Y~., DATA[DATA$Z == 100,], mean)
    tmp2 = aggregate(Y~., DATA[DATA$Z == 50,], mean)
    tmp1$X2 = tmp2$X
    tmp1$Y2 = tmp2$Y
    
    graphics.off()
    ggplot(DATA, aes(x = factor(X), y = Y, fill = Z)) +
        geom_boxplot(width = 0.5, outlier.shape = NA) +
        geom_segment(data = tmp1,
                     aes(x = as.numeric(factor(X)) - 0.125, y = Y,
                         xend = as.numeric(factor(X2)) + 0.125, yend = Y2)) +
        facet_grid(Gp ~ Fc)
    

提交回复
热议问题