Skip to content

Instantly share code, notes, and snippets.

@trinker
Created March 24, 2017 21:09
Show Gist options
  • Save trinker/51c63e9dd7baeb001904f7c77bf7c51c to your computer and use it in GitHub Desktop.
Save trinker/51c63e9dd7baeb001904f7c77bf7c51c to your computer and use it in GitHub Desktop.
Bar in Bar
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(tidyverse, cowplot)
cols <- c(lightblue = '#62B1F6', darkblue = '#27408B')
dat <- data_frame(
type = state.name[1:18],
Total = c(522, 464, 332, 185, 90, 811, 121, 242, 258, 175, 346, 217, 186, 133, 144, 136, 53, 306),
Sub = c(6, 6, 13, 19, 21, 11, 7, 8, 16, 29, 78, 71, 90, 68, 90, 99, 35, 14)
)
plot1 <- dat %>%
gather(var, val, Total:Sub) %>%
ggplot(aes(type, val, fill =var)) +
geom_bar(stat = 'identity') +
scale_fill_manual('', values = unname(rev(cols)))
leg <- get_legend(plot1)
dat %>%
ggplot() +
geom_bar(data = dat, stat = 'identity', aes(type, Total), width = .8, fill = cols[1]) +
geom_bar(data = dat, stat = 'identity', aes(type, Sub), width = .4, fill = cols[2]) +
theme_bw() +
scale_y_continuous(expand = c(0, 0), limits = c(0, 850)) +
labs(y = 'Count', x = 'State') +
theme(
axis.ticks.y = element_line(color = 'grey60'),
axis.ticks.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.border = element_rect(fill =NA, color = 'grey70')
) +
annotation_custom(leg, xmin = 17, xmax = 17, ymin = 765, ymax = 765)
@trinker
Copy link
Author

trinker commented Mar 24, 2017

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment