Hi everyone, in this video I'm going to go over a short R demo on how to create a plot called a
dumbbell plot. This is going to be what my example code will create. A dumbbell plot is a plot
that is very convenient for visualizing pairwise comparisons across multiple groups. I just have
simulated data in my example but I have simulated data for the 50 states in the United States
and then this visualization of a dumbbell plot would show you the pairwise differences between
each state. And so this pairwise difference, for example, a lot of times can be comparing
across years. The blue would be the earlier time point and then the red would be a second later
time point. In my example, the difference between the two points will be region, so rural and urban
areas in the different states, but it could be any pairwise comparison. And again like I mentioned
a little while ago, a dumbbell plot really efficiently can show multiple pairwise
comparisons. This is showing really clearly the 50 different pairwise comparisons and it would
be easy to look up what state you want and also the data is ordered kind of from left to right as
well. I'll go over how to do a plot like this in R and I have the code demonstration in an
Rpubs document and I already have it published online. I will put a link to this Rpubs document
in the description box below. And with that, let's go ahead and get started with the demo. I'm
going to first load in these three packages. This data visualization is going to be made in ggplot2
and then I'm going to load in my sample data. I have it printed in the Rpubs document, but this
is just simulated data. This data doesn't refer to anything in real life. But basically you want to
set up your dataset or your data frame using the long form. What that means is you're going to have
repeated measurements. In this case, you're going to have two repeated measures for every level
in a variable, in my example it's called State. But you see that there are two states for every
two measurements that you have per state. That type of having it in a long format versus a
wide format is what we're going to use to do the data visualization in this example. And then you
have the two measurements because you're going to have either an earlier or later time point,
or in my case an urban and a rural region time point and that's given in a variable that I'm
calling region. This is what's in the legend at the top. Then the measurement variable in my data
frame is the actual output. This is the outcome that's actually plotted on the plots. The way
I simulated the data was I put a smaller mean for the urban and a larger mean for the rural.
That's why you're going to see the measurements consistently be smaller for urban compared to
rural in the sample data. And then you also need a column in your data frame called 'paired' and this
paired variable is going to be used in ggplot2 to be able to figure out what the pairings are.
Because you have repeated measurements in the rows of State, you want to pair them up
using this paired variable. Basically you want to have two numbers and the two numbers have to
pair to the same state. For example, we have one and one, which both refer to the state of
Alabama. We're going to use this for ggplot to know that these two values correspond to
the pairwise difference in the same state. Same thing with 2, 2 for Alaska. We're going to use
that column in the data frame later on in ggplot2 to actually create and let ggplot know that these
two measurements have to be on the same line. And then this code here is just given to you guys so
you can see how I performed the simulation. This is how I simulated the data. I simulated
from the normal distribution for both rural and urban with different variances, a little bit,
but basically the mean for urban observations has a mean of 65 from the normal distribution and then
the observations from rural have a mean of 95. That's why the points are randomly scattered but
you see that rural tends to be larger than urban because that was how I simulated it. I'm not going
to run those lines of code. This next line of code actually will run and fit the dumbbell plot that
you see here. We're going to pass in df, which is our data frame, to ggplot2 and then we're going to
pass in 'measurement' for X. We're going to plot the differences basically horizontally instead of
vertically. You can do it either way but in this case, in my examples, I'm doing it horizontally. X
has the outcome measurements. And then this line of code says that you're going to plot the
different states on the Y-axis. And reorder gives you the ordering so that the states are ordered
basically from biggest to smallest. It kind of goes to the biggest values on the right-hand side
and then the smallest values on the left-hand side. There's some variation at each level,
like for example this one varies a little bit, but it basically provides a nice ordering. You can
test it without the order option and you'll see that it would kind of just give you the states in
a more random order, or in the order that you pass in, but this reordering gives you a nice ordering
to the different levels and it will make it easier for you to make comparisons across states.
And then this line of code is going to plot the actual line between the pairs of points.
We see that I wanted a gray line. This is producing the gray lines that connect the
two points for each pair of observations. The points are colored by region. We have rural and
urban points that are colored and I'm just using the default coloring of pink and blue
that ggplot2 uses. And then this size parameter will change the size of the dots. Right now,
I have it at a size three. Then this provides the labels for the axes. I just actually have a Y-axis
label of State and then a title, which is "Insert Title Here" for whatever you want the title to be.
I'm using the black and white theme and then I say to put the legend at the top. Otherwise it
would put it on the right hand side. And that is basically the code for the dumbbell plot. I also
wanted to mention in this video that this data visualization is a nice alternative to grouped
bar charts. This is the same data but plotted using grouped bar charts. Grouped bar charts
a lot of times are also used to make pairwise comparisons but the data visualization does not
look good if you have several groups. For example, 50 groups--when you're comparing pairwise data for
50 states, that creates a lot of bars and you would have to print this data visualization out
very, very wide for it to even look legible. This is the same data printed as a grouped bar chart,
but you can see that the data is much more legible using a dumbbell plot. And then in this line of
code, I also wanted to show how you can put labels at the bottom of the dumbbells, or at the bottom
of the dots. I'm going to run this line of code. It's using the same data but basically what it
does is it adds a label to the bottom of each dot. This lets you make more precise comparisons than
a data visualization without the dots. Without the dots, you could just make kind of relative
comparisons, but this way if you actually see the values on the data, you can do the subtraction in
your mind and get the precise comparisons. To do that, you just add these final two lines of code.
You're just going to add this part that says "geom text". The text is going to be the measurement
for rural and urban for each state and the "vadjust" means to put it slightly below the dot.
If you wanted it to be horizontally adjusted, you would put hjust but it's easier to put it
just below the dots, so I'm using vjust instead. And then this final line of code will show you
how to print out the data visualization if you want it in a long or wide format. In this case,
this data visualization should be a little bit longer or should be a little bit more
stretched out. You can see the states and the values more clearly instead of having like the
labels, for example, overlapping each other. If I run this line of code, I'm saying to make the
data visualization longer than it is wide. And then if I check the PDF that I just produced,
it's going to give me something that looks like this. This is a little bit long, but you see that
in this case the different states are now spread out nicely and none of the values are cut off at
the bottom as well. Something like this could be a data visualization on an entire page and it would
display the data nicely. And that's all I have for this video and thank you guys for watching!
*chiptune music*