Fine tuning ggplot2's geom boxplot

后端 未结 2 943
执念已碎
执念已碎 2021-01-12 14:54

I have this data.frame

my.df = data.frame(mean = c(0.045729661,0.030416531,0.043202944,0.025600973,0.040526913,0.046167044,0.029352414,0.021477789,0.02758052         


        
2条回答
  •  再見小時候
    2021-01-12 15:28

    You can use geom_segment to add center lines. I use an alpha parameter for the start and end of the line centered in replicate.

    enter image description here

    library(ggplot2)
    alpha=0.3
    
    p1 = ggplot(data = my.df, aes(factor(replicate), 
                                  color = factor(parent.origin)))
    p1 = p1 + geom_boxplot(aes(fill = factor(parent.origin), 
                               lower = mean - std.dev, 
                               upper = mean + std.dev, 
                               middle = mean, 
                               ymin = mean - 3*std.dev, 
                               ymax = mean + 3*std.dev), 
                           width = 0.3, 
                           linetype="dotted",
                           stat="identity") + 
      facet_wrap(~group, ncol = 4)+ 
      scale_color_manual(values = c("red","blue"),labels = c("maternal","paternal"),
                         name = "parental allele")+
      scale_fill_manual(values = c("red","blue")) +
      geom_segment(aes(x = replicate-alpha, 
                       y = mean, 
                       xend = replicate+alpha, 
                       yend = mean),
                   inherit.aes=FALSE,color="black",size=1.5)
    
    
    p1
    

提交回复
热议问题