Created
November 10, 2018 17:19
-
-
Save dhicks/79ffe114fc8373ec813b05585b39eb96 to your computer and use it in GitHub Desktop.
Cleveland Dot Plots or Dumbell Plots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Cleveland Dot Plots or Dumbell Plots | |
## This gist notes some approaches for drawing Cleveland dot plots or dumbell plots in `ggplot` | |
library(tidyverse) | |
n = 10 | |
dataf = tibble(x = rep(1:n, 2), | |
category = c(rep('A', n), rep('B', n)), | |
value = x + 2*(category == 'B')) | |
## Line segments w/o arrows are pretty easy | |
ggplot(dataf, aes(x, value, color = category)) + | |
stat_summary(color = 'black', | |
fun.ymin = min, fun.ymax = max, | |
geom = 'linerange') + | |
geom_point() | |
## Even easier using geom_line() | |
ggplot(dataf, aes(x, value, color = category)) + | |
geom_line(aes(group = x), color = 'black') + | |
geom_point() | |
## Say we want arrows from A to B, eg, to show change between two points in time or between a sample and subsample | |
ggplot(dataf, aes(x, value, color = category)) + | |
geom_line(aes(group = x), color = 'black', | |
arrow = arrow()) + | |
geom_point() | |
## Arrow direction doesn't follow the order of the levels in `category`? | |
ggplot(dataf, aes(x, value, color = fct_rev(category))) + | |
geom_line(aes(group = x), color = 'black', | |
arrow = arrow()) + | |
geom_point() | |
## Just change where arrow() places the ends | |
ggplot(dataf, aes(x, value, color = category)) + | |
geom_line(aes(group = x), color = 'black', | |
arrow = arrow(ends = 'first')) + | |
geom_point() | |
## geom_segment() + spread() is another approach | |
ggplot(dataf, aes(x, value, color = category)) + | |
geom_segment(data = spread(dataf, key = category, value = value), | |
aes(xend = x, y = A, yend = B), color = 'black', | |
arrow = arrow()) + | |
geom_point() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment