Skip to content

Instantly share code, notes, and snippets.

@leeolney3
Last active November 1, 2021 17:17
Show Gist options
  • Save leeolney3/7ff6c982d7928a554ad70fc8a367cea8 to your computer and use it in GitHub Desktop.
Save leeolney3/7ff6c982d7928a554ad70fc8a367cea8 to your computer and use it in GitHub Desktop.
TidyTuesday 2021/W41
library(tidyverse)
library(janitor)
library(ggtext)
library(geofacet)
library(biscale)
library(cowplot)
nurses <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-05/nurses.csv') %>% clean_names()
nurses$st <- state.abb[match(nurses$state, state.name)]
df1 = nurses %>%
filter(year==2020) %>%
mutate(st=ifelse(state=="District of Columbia","DC",st)) %>%
mutate(st=ifelse(state=="Puerto Rico","PR",st)) %>%
drop_na(st) %>%
select(state, st, year, total_employed_rn, hourly_wage_median) %>%
bi_class(x=hourly_wage_median, y=total_employed_rn, style="quantile",dim=3)
# state tile map function reference: https://medium.com/@NickDoesData/visualizing-geographic-data-in-r-fb2e0f5b59c5
create_gradient_state_tile_map <- function(state, value, title, subtitle, caption, legend_title, state_grid='us_state_with_DC_PR_grid2') {
df <- as.tibble(data.frame(state, value))
fig <- df %>%
mutate(x = 1) %>%
mutate(label_y = .5) %>%
mutate(label_x = 1) %>%
ggplot()+
geom_bar(mapping=aes(x=x, fill=value), width=.4) +
facet_geo(~ state, grid=state_grid) +
labs(title=title, subtitle=subtitle, caption=caption) +
geom_text(aes(x=label_x, y=label_y, label=state, color=value),size=3, show.legend=F, family="sans")
return(fig)
}
# main plot
p1 = create_gradient_state_tile_map(df1$st, df1$bi_class,
title='US-based Registered Nurses Employment and Wage in 2020', legend_title = "",
subtitle="<span style = 'color:#012a4a;'><b>Total employed registered nurses</b></span> and <span style = 'color:#012a4a;'><b>median hourly wage</b></span>, by US state\n",
caption="Note: Data from Guam and Virgin Islands are not presented<br>#TidyTuesday Week 41 | Data from Data.World") +
bi_scale_fill(pal="DkCyan",dim=3, guide="none") +
scale_color_manual(values=c("grey10","grey10","white","white","white","white","white","white","white")) +
theme_void(base_size=10, base_family = "sans") +
theme(strip.text.x = element_blank(),
plot.margin = unit(c(.5,4,.5,2), "cm"),
plot.title=element_text(size=14, face="bold", color="#012a4a"),
plot.subtitle=element_markdown(size=8, color="#011c31", margin=margin(t=5,b=18)),
legend.title=element_text(size=9),
plot.caption = element_markdown(size=5.6, color="#011c31",margin=margin(t=30), lineheight=1.5, hjust=0)) +
guides(fill = guide_colorbar(title="Count",
title.position = "top",
barwidth = unit(.5, "lines"),
barheight = unit(10, "lines")))
# legend
p2 = bi_legend(pal = "DkCyan",
dim = 3,
ylab = "Total employed",
xlab = "Median hourly wage",
size = 2.5) +
theme(panel.border = element_blank(),
axis.text = element_blank(),
axis.title.x = element_text(size = 6, family="sans",
color = "#011c31", margin=margin(t=-5)),
axis.title.y = element_text(size = 6, family="sans",
color = "#011c31", margin=margin(r=-5)),
legend.text = element_text(size = 6),
plot.background = element_blank(),
legend.text.align = 0)
# combine plot and legend
ggdraw() +
draw_plot(p1, 0, 0, 1, 1) +
draw_plot(p2, 0.72, 0.04, 0.25, 0.25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment