Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading