Dumbbell plots can be created using 2 ggplot2 building blocks: geom_point and geom_line.
So let's start building from the ground up with some examples in each before combining to make a dumbbell plot.
2/15
1. geom_point
Plots in ggplot2 start with the ggplot function. We then add + successive layers with geometry functions before customising the appearance further.
geom_point is used to create scatter plots and other point charts.
3/15
Notice I placed the x and y variables inside the aes function.
I found aesthetics hard to understand at first - you read lots of talk of mapping variables and visual cues which didn't make sense to me then.
But I think there is an easier way to understand them.
4/15
Anything on a chart (a line, point, colour, shape, size etc) which changes with the data, place inside aes().
Anything which doesn't vary with data (a colour, shape, size, etc), place outside aes().
It's easier to see with an example.
5/15
In plot 1 colour is placed inside aes so the colour varies with the data. Here it corresponds to the group1 value since colour = group1.
In the second plot colour is placed outside aes. The colour is red regardless of the data values.
6/15
2. geom_line
geom_line works similarly to geom_point.
You can add 1 or more lines and easily combine with geom_point.
7/15
The dummy celsius dataset was in what is known as wide format.
We can put this into long format using pivot_longer from tidyr. Long format will be important for our dumbbell plot. There is now a row for high and for low for each month.
8/15
From long format we can replicate the last plot with similar code.
Notice to get the correct colours I have created a named vector called chart colours and passed it to scale_colour_manual. This approach even throws in a legend.
Now we are ready for the dumbbell plot.
9/15
3. Dumbbell plots
We know about geom_point, geom_line, aes and long format. We can combine these to make a dumbbell plot.
Let's start with the points.
10/15
We will group our data by high and low temperature with a point for the value. The y axis will have the months.
The first stab at it is not bad but the months go from December to January.
We can sort this by reordering the month name factor level.
11/15
Next we add the lines.
Here we use the group argument and place it inside aes. This will group the lines by month_name. The other aes arguments are the same.
The first attempt is ok but the lines overlap the points. Not to worry, we can swap them.
12/15
Finally we can make some tweaks to make the chart look better. We could do plenty more but the objective here was to learn about dumbbell plot, not the ins and outs of good design.
13/15
TL;DR
1. Dumbbell plots are a great way to communicate insights. 2. These can be created with ggplot2 by combining geom_line and geom_point. 3. You need to know a bit about aesthetics and long format data.
14/15
Thanks for reading, if you liked this thread follow me @neilgcurrie for mainly R and data science stuff (though I make no promises).
See the README file to get the code for this thread and others:
Big data is any data which is too large to be loaded into memory all in one go. Ever tried to read a large dataset into R and everything fell over or slowed waaaaaay down? This is where Spark comes in.
1/18
Spark is an open source tool for processing big data. It splits data into partitions for processing to overcome RAM limitations and writes to disk when needed. It is complicated but Spark handles most of the difficult parts. And you can work with Spark directly from R.