Le Paul
Le Paul

Reputation: 67

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

enter image description here

Upvotes: 0

Views: 1178

Answers (1)

stefan
stefan

Reputation: 124013

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)

Upvotes: 2

Related Questions