Last active
August 9, 2016 00:56
-
-
Save mrecos/e0f5a2c12a042bd65c27dde56b5895d8 to your computer and use it in GitHub Desktop.
Code for ggplot2 implimentation of dog vs cat plot posted by @hpster. using Dev (github) version of ggplot2 and ggalt
This file contains hidden or 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
library("ggplot2") | |
library("ggalt") | |
library("dplyr") | |
color_function <- colorRampPalette(c("cadetblue3", "darkolivegreen3")) | |
num_books = 35 | |
books <- paste0("book",1:num_books) | |
dogcat <- runif(length(books), -2.5, 2.5) | |
dat <- data.frame(books = books, dogcat = dogcat) | |
dat$books <- factor(books, levels = dat$books[rev(order(dat$dogcat))]) | |
dat <- dat[rev(order(dat$dogcat)),] | |
dat$fill_color <- color_function(nrow(dat)) | |
ggplot() + | |
geom_bar(data = dat, | |
aes(x = books, y = dogcat, fill = (fill_color)), | |
stat = "identity") + | |
# use second geom_bar is you want the >= 0 side to be a different color, otherwise ignore. | |
# geom_bar(data = dplyr::filter(dat, dogcat >= 0), | |
# aes(x = books, y = dogcat, fill = fill_color), | |
# stat = "identity") + | |
coord_flip() + | |
theme_bw() + | |
scale_y_continuous(breaks = c(-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2), | |
labels =c ("","1.5x\ncat people","","","Equal","","","","2x\nDog People")) + | |
scale_fill_manual(values = rev(dat$fill_color)) + | |
labs(title="Books")+ | |
theme( | |
panel.background = element_rect(fill = "gray20"), | |
plot.background = element_rect(fill = "gray20"), | |
panel.border = element_blank(), | |
panel.grid.minor = element_blank(), | |
panel.grid.major.y = element_blank(), | |
panel.grid.major.x = element_line(colour = "gray80", linetype = "dashed"), | |
axis.text = element_text(colour = "gray90"), | |
axis.ticks.x = element_line(color = "gray80"), | |
axis.ticks.y = element_blank(), | |
axis.title = element_blank(), | |
plot.title = element_text(color = "gray90", hjust = 0.5), | |
legend.position = "none" | |
) | |
ggsave(filename = "dog_vs_cat_books.png", width = 4, height = 6) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment