Created
April 16, 2017 17:27
-
-
Save trinker/982d246e83fc0fbc5276a21424ce85be to your computer and use it in GitHub Desktop.
Dumbell plot
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
dat <- data.frame( | |
freq = c(37, 88, 47, 89, 28, 68, 65, 98, 68, 86, 50, 87, 59, 82, 45, 65, 52, 32, 59, 47, 68, 78, 39, 31, 64, 68) | |
) | |
dat[["sector"]] <- rep(c("U.S. adults", "AAAS scientist"), nrow(dat)/2) | |
field <- c("Biomedical sciences", "Climate, energy, space sciences") | |
dat[["field"]] <- factor(rep(field, c(10, 16)), levels=field) | |
items <- c( | |
"Safe to eat genetically\nmodified foods", | |
"Favor the use of animas\nin research", | |
"Safe to eat foods grown\nwith pesticides", | |
"Humans have evolved over\ntime", | |
"Childhood vaccines such\nas MMR should be required ", | |
"Climate change is mostly\ndue to human activity", | |
"Growing world population\nwill be a major problem", | |
"Favor building more\nnuclear power plants", | |
"Favor more offshore\ndrilling", | |
"Astronaughts essential for\nfuture of U.S. space program", | |
"Favor increased use of\nbioengineered fuel", | |
"Favor increased use of\nfracking", | |
"Space station has been\na good investment for U.S." | |
) | |
dat[["Item"]] <- factor(rep(items, each=2), levels=rev(items)) | |
library(dplyr) | |
diffs <- dat %>% | |
group_by(Item, field) %>% | |
summarise( | |
diff = abs(diff(freq)), | |
mid = mean(freq), | |
xmin = min(freq), | |
xmax=max(freq) | |
) | |
ext <- rep("", nrow(diffs)) | |
ext[c(5, 13)] <- "point gap" | |
ext2 <- rep("", nrow(diffs)) | |
ext2[c(5, 13)] <- "%" | |
#amt <- 2 | |
amt <- 1.5 | |
library(ggplot2) | |
ggplot(dat, aes(y = Item, x=freq)) + | |
geom_segment(data=diffs, aes(x=xmin, xend=xmax, y=Item, yend=Item), color="royalblue2", size=5.5) + | |
geom_point(color = "navy", size=5.5) + | |
geom_point(shape=21, color = "navy", size=4.8, aes(fill=sector)) + | |
facet_grid(field~., space="free", scales="free") + | |
scale_fill_manual(values = c("white", "darkblue"), guide=FALSE) + | |
theme_bw() + | |
coord_cartesian(xlim = c(24, 102)) + | |
ylab(NULL) + xlab(NULL) + | |
geom_text(data = diffs, color = "white", | |
aes(label = paste(diff, ext), x=mid, y=Item), | |
size=3, vjust = .4, hjust=.2) + | |
geom_text(data = diffs, color = "grey60", aes(label = paste0(xmin, ext2), x=xmin -amt, y=Item), | |
size=3, vjust = .4, hjust=1) + | |
geom_text(data = diffs, color = "grey60", aes(label = paste0(xmax, ext2), x=xmax + amt, y=Item), | |
size=3, vjust = .4, hjust=0) + | |
theme( | |
axis.text.y = element_text(hjust=0), | |
axis.text.x = element_blank(), | |
axis.ticks = element_blank(), | |
panel.grid.minor.x = element_blank(), | |
panel.grid.major.x = element_blank(), | |
panel.grid.major.y = element_blank(), | |
panel.border = element_blank(), | |
strip.background = element_rect(color=NA, fill="grey75"), | |
strip.text.y = element_text(size=12, face="bold", hjust=.05), | |
panel.spacing = unit(1, "lines") | |
) + | |
geom_hline(color="grey60", linetype="dotted", size=.7, yintercept=seq(.5, to=8.5, by=1)) |
Author
trinker
commented
Apr 16, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment