2019-04-25

Plot median and quartiles in ggplot2 using geom_pointrange

Problem

We would like to plot the interquartile range (IQR) and the median in ggplot2.

Solution

In this case we use the function geom_pointrange. We need to specify the interval (interquartile range) with the arguments fun.ymin and fun.ymax, and fun.y to plot the median.

library(ggplot2)
ggplot(data = diamonds) +
  geom_pointrange(mapping = aes(x = cut, y = depth),
                  stat = "summary",
                  fun.ymin = function(z) {quantile(z,0.25)},
                  fun.ymax = function(z) {quantile(z,0.75)},
                  fun.y = median)

Results

Related posts

Spanish version

References

No hay comentarios:

Publicar un comentario

Nube de datos