Created
August 22, 2020 17:34
-
-
Save farach/5d89f25edde5a35ae527385ce8b2b76f to your computer and use it in GitHub Desktop.
NITDR Budget Plot
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
| # install.packages("pacman") | |
| pacman::p_load(tidyverse, RColorBrewer, janitor) | |
| nitrd <- read.csv("https://www.nitrd.gov/data/NITRD_Budget_Tables.csv") | |
| nitrd_fill <- nitrd %>% | |
| janitor::clean_names(.) %>% | |
| mutate( | |
| budget = as.numeric(str_remove(as.character(budget), ",")), | |
| fy = as.numeric(fy) | |
| ) %>% | |
| group_by(fy, pca) %>% | |
| summarize(budget = sum(budget, na.rm = TRUE)) %>% | |
| filter(pca != "All PCAs") %>% | |
| pivot_wider(names_from = pca, values_from = budget) %>% | |
| mutate(across(everything(), ~replace_na(.x, 0))) %>% | |
| pivot_longer(cols = -fy, names_to = "pca", values_to = "budget") %>% | |
| mutate(total = sum(budget, na.rm = TRUE), | |
| percentage = budget/sum(budget, na.rm = TRUE)) | |
| color <- grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)] | |
| n <- length(unique(nitrd_fill$pca)) | |
| ggplot(nitrd_fill) + | |
| geom_area(aes(fy, percentage, fill = pca), | |
| alpha = 0.5, size = 1, color = "black") + | |
| labs( | |
| title = "NITDR Budget", | |
| x = "Fiscal year", | |
| y = "% of budget", | |
| fill = "Program \ncomponent \nareas:", | |
| caption = "source: www.nitrd.gov" | |
| ) + | |
| theme_minimal() + | |
| theme(axis.title.x = element_text(hjust = .95, size = 9), | |
| axis.title.y = element_text(hjust = 1, size = 9), | |
| plot.title = element_text(hjust = 0.5, size = 20), | |
| legend.position = "bottom" | |
| ) + | |
| scale_y_continuous(labels = scales::percent) + | |
| scale_fill_discrete(type = sample(color, n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment