Dumbbell Plots in ggplot2

How to make Dumbbell Plots in ggplot2 with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Default dumbell plot

To make the dumbell plot use geom_line() and geom_point() functions.

For making dumbbell plot, let us subset the data for just two years 1952 and 2007. Also, we focus on one of the continents in the gapminder data.

With this data we can make dumbbell plot to compare life expectancy change from 1952 to 2007 for all asian countries. We make dumbbell plot by plotting points for each time point and connect them with a line for each country. In order to connect the points, we need specify which rows or countries need to be connected. We create a new variable that specifies the group corresponding to each country.

library(plotly)
library(ggplot2)
library(tidyverse)
theme_set(theme_bw())

gapminder <- read_csv("https://raw.githubusercontent.com/datavizpyr/data/master/gapminder-FiveYearData.csv")
df <- gapminder %>%
  filter(year %in% c(1952,2007)) %>%
  filter(continent=="Asia") 

df <- df %>%
  mutate(paired = rep(1:(n()/2),each=2),
         year=factor(year))

p <- df %>% 
  ggplot(aes(x= lifeExp, y= country)) +
  geom_line(aes(group = paired))+
  geom_point(aes(color=year), size=4) +
  theme(legend.position="top")


ggplotly(p)

Changing order

You can reorder the dumbbell plot by life expectancy values using reorder() function to make it easy to read the plot.

library(plotly)
library(ggplot2)
library(tidyverse)
theme_set(theme_bw())

gapminder <- read_csv("https://raw.githubusercontent.com/datavizpyr/data/master/gapminder-FiveYearData.csv")
df <- gapminder %>%
  filter(year %in% c(1952,2007)) %>%
  filter(continent=="Asia") 

df <- df %>%
  mutate(paired = rep(1:(n()/2),each=2),
         year=factor(year))

p <- df %>% 
  ggplot(aes(x= lifeExp, y= reorder(country,lifeExp))) +
  geom_line(aes(group = paired))+
  geom_point(aes(color=year), size=4) +
  labs(y="country")

ggplotly(p)

Improving readability

Change the colour of the line between the points to grey, to introduce contrast and highlight the change between the points.

library(plotly)
library(ggplot2)
library(tidyverse)
theme_set(theme_bw())

gapminder <- read_csv("https://raw.githubusercontent.com/datavizpyr/data/master/gapminder-FiveYearData.csv")
df <- gapminder %>%
  filter(year %in% c(1952,2007)) %>%
  filter(continent=="Asia") 

df <- df %>%
  mutate(paired = rep(1:(n()/2),each=2),
         year=factor(year))

p <- df %>% 
  group_by(paired) %>%
  ggplot(aes(x= lifeExp, y= reorder(country,lifeExp))) +
  geom_line(aes(group = paired),color="grey")+
    geom_point(aes(color=year), size=4) +
  labs(y="country")

ggplotly(p)

To change colour, use scale_color_brewer().

To remove the grey lines in the background, use theme_classic().

library(plotly)
library(ggplot2)
library(tidyverse)
theme_set(theme_bw())

gapminder <- read_csv("https://raw.githubusercontent.com/datavizpyr/data/master/gapminder-FiveYearData.csv")
df <- gapminder %>%
  filter(year %in% c(1952,2007)) %>%
  filter(continent=="Asia") 

df <- df %>%
  mutate(paired = rep(1:(n()/2),each=2),
         year=factor(year))

p <- df %>% 
  ggplot(aes(x= lifeExp, y= reorder(country,lifeExp))) +
  geom_line(aes(group = paired),color="grey")+
    geom_point(aes(color=year), size=6) +
  labs(y="country")+
  theme_classic()+
  scale_color_brewer(palette="Accent", direction=-1)

ggplotly(p)

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)