Diverging Dot Plot and Lollipop Charts – Plotting Variance with ggplot2

Creating the Dot Plot Variance chart

The data preparation was used in the previous blog entitled: Diverging Bar Charts – Plotting Variance with ggplot2.

Refer to that if you need to know how to create the data prior to this tutorial.

Setting up the Dot Plot Variance chart

  1. library(ggplot2)
  2. ggplot(mtcars, aes(x=CarBrand, y=mpg_z_score, label=mpg_z_score)) +
  3. geom_point(stat='identity', aes(col=mpg_type), size=6) +
  4. scale_color_manual(name="Mileage (deviation)",
  5. labels = c("Above Average", "Below Average"),
  6. values = c("above"="#00ba38", "below"="#0b8fd3")) +
  7. geom_text(color="white", size=2) +
  8. labs(title="Diverging Dot Plot (ggplot2)",
  9. subtitle="Z score showing Normalised mileage", caption="Produced by Gary Hutson") +
  10. ylim(-2.5, 2.5) +
  11. coord_flip()

Setting up the Dot Plot Variance chart

This is very similar to the previous plot we created in the previous post, however there are a few differences. The main difference is that we use a geom_point() geometry and set the colour of the points based on whether the said point deviates above and below the average. In addition, we use the geom_text() to set the colour of the text in the points to white and specify the size of the text. The final difference is that I have added a Y limit (ylim) range of -2.5 standard deviation to positive 2.5 standard deviations.

Running this block of code, along with the data preparation code, will give you a chart that looks as below:









This blog was written by Gary Hutson, Principal Analyst, Activity & Access Team, Information & Insight at Nottingham University Hospitals NHS Trust, and was originally posted here.