Dumbbell plots

ggplot Beginner dumbbell plots

Before and After Changes with Dumbbells.

Soundarya Soundararajan true
08-17-2021

One of the ways to represent before and after changes is using dumbbells

Lets jump into it.

library(tidyverse)
library(ggalt) # for the dumbbells
library(ggtext)

We will work on the ChickWeight dataset for today. We will explore how much the chick weights changed from baseline to day 10 grouped by the 4 diets.

And we will create something like this

Lets create the dataframe of interest.

There are many time points of data available for weight, we choose day 0 and day 10 for simplicity.

We need the day 0 and day 10 weights in separate columns to make things easier, so we use pivot_wider from the tidyr which is a part of tidyverse library.

Cut me the chase and take me to the final plot

Create the dataframe

Filter

# this is how to filter only the two times of interest
ChickWeight %>%
  filter(Time == 0 | Time == 10) %>% 
  rmarkdown::paged_table()# to avoid the entire dataset beimg displayed, i add pages

Note that it is not saved as a dataframe yet. I will describe the steps one-by-one.

pivot wider

ChickWeight %>%
  filter(Time == 0 | Time == 10) %>% # after filtering, we group them
  group_by(Diet, Time, Chick) %>%
  pivot_wider(
    id_cols = c(Diet, Chick),
    names_from = Time,
    values_from = weight, names_prefix = "time_"
  ) %>% 
  rmarkdown::paged_table()

Now we shall summarise the weight info and create the dataframe

summarise mean

df <- ChickWeight %>%
  filter(Time == 0 | Time == 10) %>% # after filtering, we group them
  group_by(Diet, Time, Chick) %>%
  pivot_wider(
    id_cols = c(Diet, Chick),
    names_from = Time,
    values_from = weight, names_prefix = "time_"
  ) %>%
  mutate(wtdiff = time_10 - time_0) %>% # we add a difference column
  group_by(Diet) %>%
  summarise(
    mean_0 = mean(time_0),
    mean_10 = mean(time_10, na.rm = TRUE),
    mean_diff = mean(wtdiff, na.rm = TRUE)
  )
df
# A tibble: 4 × 4
  Diet  mean_0 mean_10 mean_diff
  <fct>  <dbl>   <dbl>     <dbl>
1 1       41.4    93.1      51.5
2 2       40.7   108.       67.8
3 3       40.8   117.       76.3
4 4       41     126        85  

Now time for plots!

Initiate the dumbbells

For the dumbbells we need to specify what the x and xends are, here we need the baseline weight to be on the left and day 10 weight on the right.

ggplot(df) +
  geom_dumbbell(
    aes(
      x = mean_0,
      xend = mean_10,
      y = Diet
    )
  )

We got the coordinates right, now time for some tweaking.

Improving the dumbbells

We are adding size and colors for the dumbbells

p <- ggplot(df) +
  geom_dumbbell(
    aes(
      x = mean_0,
      xend = mean_10,
      y = Diet
    ),
        # size and color of the connecting lines

    size = 1, color = "#676875", alpha = 0.5, 
        # size and color of the left half of the dumbbell

    size_x = 5, colour_x = "#F51F18", 
    # right side of the dumbbell
    size_xend = 5, colour_xend = "#1E4CE8"
  ) + 
  theme_classic()
p

How about we add the legends?

Mark the legends

We need to mark the legends at only one place, so we will filter the data.

p <- p + geom_text(
  data = filter(df, Diet == 4), # if you need the labels at diet 1, tweak accordingly
  aes(
    x = mean_0 + 6,
    y = Diet,
    label = "Baseline weight"
  ), 
  color = "#F51F18",
  vjust = -1.5 # this is vertical justification for the labels
) +
  geom_text(
    data = filter(df, Diet == 4),
    aes(
      x = mean_10 - 8,
      y = Diet,
      label = "Weight on 10th day"
    ), color = "#1E4CE8",
    vjust = -1.5
  ) +

  theme(legend.position = "none")
p

Let’s up our game by adding the differences which we already have in our df

Add a column of mean difference

p <- p + geom_label(
  data = df,
  aes(
    x = mean_10 + 7,
    y = Diet,
    label = str_glue("{round(mean_diff,0)} kg")
  )
)
p

Lets add axes titles and captions

Final Plot

p + labs(
  x = "Mean Weights of Chicken",
  caption = "Difference in weights are given in text boxes"
)

Distill is a publication format for scientific and technical writing, native to the web.

Learn more about using Distill at https://rstudio.github.io/distill.

Citation

For attribution, please cite this work as

Soundararajan (2021, Aug. 17). My R Space: Dumbbell plots. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2021-08-17-dumbell-plots/

BibTeX citation

@misc{soundararajan2021dumbbell,
  author = {Soundararajan, Soundarya},
  title = {My R Space: Dumbbell plots},
  url = {https://github.com/soundarya24/SoundBlog/posts/2021-08-17-dumbell-plots/},
  year = {2021}
}