Fine tuning ggplot2's geom boxplot

后端 未结 2 944
执念已碎
执念已碎 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
    
    0 讨论(0)
  • 2021-01-12 15:29

    Here's what worked for me:

    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), position = position_dodge(width = 0), width = 0.5, 
             alpha = 0.5, stat="identity") + 
         facet_wrap(~group, ncol = 4)+
         scale_fill_manual(values = c("red","blue"),labels = c("maternal","paternal"),
             name = "parental allele")+
         scale_colour_manual(values = c("red","blue"),labels = c("maternal","paternal"),
             name = "parental allele")
    

    The width value within the position_dodge parameter determines the separation between the red and blue boxes per each replicate, and the other width parameter controls the width o he box. The alpha value controls transparency.

    0 讨论(0)
提交回复
热议问题