Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleveland dot plot in ggplot2

In the recent TIMSS report that I happened to come across, there's a plot (shown below) that in my opinion is very communicative. I've read that such plots are called Cleveland dot plots, though this one adds confidence intervals as well. I was wondering if it can be reproduced in ggplot2 or matplotlib. All hints are welcome. plot
(source: timss2015.org)

like image 915
John Smith Avatar asked Oct 30 '25 01:10

John Smith


1 Answers

Using the iris data set:

library(dplyr)
library(ggplot2)

plot_data <- iris %>% 
  group_by(Species) %>% 
  summarise_each(funs(mean, sd, n(), q95=quantile(., 0.95), q75=quantile(., 3/4), q25=quantile(., 1/4),  q5 = quantile(., 0.05)), Sepal.Length) %>% 
  mutate(se = sd/sqrt(n),
         left95 = mean - 2*se,
         right95 = mean + 2*se)


ggplot(plot_data, aes(x = Species, y = mean)) +
  geom_crossbar(aes(ymin = q5, ymax = q95), fill = "aquamarine1",  color = "aquamarine1", width = 0.2) +
  geom_crossbar(aes(ymin = q25, ymax = q75), fill = "aquamarine4",  color = "aquamarine4", width = 0.2) +
  geom_crossbar(aes(ymin = left95, ymax = right95), fill = "black", color = "black", width = 0.2) +
  coord_flip() +
  theme_minimal()

enter image description here

This should give you the gist of how to use ggplot2 to accomplish this. The data you provided can be easily used, without the dplyr summarizing.

like image 132
Jake Kaupp Avatar answered Oct 31 '25 14:10

Jake Kaupp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!