Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Dumbbell chart
#1. Install all packages
install.packages("dplyr")
install.packages("tidyr")
install.packages("scales")
install.packages("ggalt")
#1. Connect with the libaries
library(dplyr)
library(tidyr)
library(scales)
library(ggplot2)
library(ggalt)
#2. Load and read the data
health <- read.csv("https://rud.is/dl/zhealth.csv", stringsAsFactors=FALSE,
header=FALSE, col.names=c("pct", "area_id"))
head(health, n=10)
areas <- read.csv("https://rud.is/dl/zarea_trans.csv", stringsAsFactors=FALSE, header=TRUE)
head(areas, n=10)
#3. Join the data
health %>%
mutate(area_id=trunc(area_id)) %>%
arrange(area_id, pct) %>%
mutate(year=rep(c("2014", "2013"), 26),
pct=pct/100) %>%
left_join(areas, "area_id") %>%
mutate(area_name=factor(area_name, levels=unique(area_name))) -> health2
head(health2, n=10)
#4. Change variables
setNames(bind_cols(filter(health2, year==2014), filter(health2, year==2013))[,c(4,1,5)],
c("area_name", "pct_2014", "pct_2013")) -> health3
head(health3, n=10)
#5. Create the chart
ggplot(health3, aes(x=pct_2014, xend=pct_2013, y=area_name, group=area_name)) +
geom_dumbbell(colour="steelblue", size=0.5, colour_xend="red", size_xend = 3,
dot_guide=TRUE, dot_guide_size=0.5, dot_guide_colour = "grey") +
scale_x_continuous(label=percent) +
labs(x=NULL, y=NULL) +
theme_tufte()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.