Home > Blockchain >  How to draw mean as a dotted line in boxplot using ggplot?
How to draw mean as a dotted line in boxplot using ggplot?

Time:04-18

I was wondering if it's possible to draw a dotted line that corresponds to the mean value of my data in a box plot.

I know that there is possible to draw shapes with stat_summary() like for example drawing a corresponding to the mean with stat_summary(fun.y=mean, shape=" ", size=1, color = "black") nearest thing is using the geom="crossbar" but this is not dotted.

The idea is to get this graphed

enter image description here

CodePudding user response:

You could achieve your desired result by setting linetype="dotted":

library(ggplot2)

ggplot(mtcars, aes(factor(cyl), mpg))  
  geom_boxplot()  
  stat_summary(geom = "crossbar", fun = "mean", linetype = "dotted", width = .75)

  • Related