-
Notifications
You must be signed in to change notification settings - Fork 1
/
tt1-fed-rnd.R
97 lines (84 loc) · 3.55 KB
/
tt1-fed-rnd.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Loading libraries
# install.packages("easypackages")
library("easypackages")
libraries("tidyverse", "tidyquant", "DataExplorer")
# Reading in data directly from github
climate_spend_raw <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-12/climate_spending.csv", col_types = "cin")
climate_spend_raw$department %>% unique()
climate_spend_raw$year %>% unique()
##plot_str(climate_spend_raw, type = 'r')
plot_intro(climate_spend_raw)
##introduce(climate_spend_raw)
variance_climate_spend <- plot_boxplot(climate_spend_raw, by = "year")
climate_spend_conditioned <- climate_spend_raw %>%
mutate(year_dt = str_glue("{year}-01-01")) %>%
mutate(year_dt = as.Date(year_dt)) %>%
mutate(test_median = median(gcc_spending)) %>%
mutate(gcc_spending_txt = scales::dollar(gcc_spending,
scale = 1e-09,
suffix = "B"
)
)
# Total spend per department per year
climate_spend_dept_y <- climate_spend_conditioned %>%
group_by(department, year_dt = year(year_dt)) %>%
summarise(
tot_spend_dept_y = sum(gcc_spending)) %>%
mutate(tot_spend_dept_y_txt = tot_spend_dept_y %>%
scales::dollar(scale = 1e-09,
suffix = "B")
) %>%
ungroup()
climate_spend_conditioned %>%
select(-c(gcc_spending_txt, year_dt)) %>%
group_by(department) %>%
summarise(total_spend_y = sum(gcc_spending)) %>%
arrange(desc(total_spend_y)) %>%
mutate(total_spend_y = total_spend_y %>% scales::dollar(scale = 1e-09,
suffix = "B",
prefix = "$")
)
climate_spend_plt_fn <- function(
data,
y_range_low = 2000,
y_range_hi = 2010,
ncol = 3,
caption = ""
)
{
plot_title <- str_glue("Federal R&D budget towards Climate Change: {y_range_low}-{y_range_hi}")
data %>%
filter(year_dt >= y_range_low & year_dt <= y_range_hi) %>%
ggplot(aes(y = tot_spend_dept_y_txt, x = department, fill = department ))+
geom_col() +
facet_wrap(~ year_dt,
ncol = 3,
scales = "free_y"
) +
#scale_y_continuous(breaks = scales::pretty_breaks(10)) +
theme_tq() +
scale_fill_tq(theme = "dark") +
theme(
axis.text.x = element_text(angle = 45,
hjust = 1.2),
legend.position = "none",
plot.background=element_rect(fill="#f7f7f7"),
) +
labs(
title = plot_title,
x = "Department",
y = "Total Budget $ Billion",
subtitle = "NASA literally dwarfs all the other departments, getting to spend upwards of 1.1 Billion dollars every year since 2000.",
caption = caption
)
}
climate_spend_plt_fn(climate_spend_dept_y,
y_range_low = 2000,
y_range_hi = 2010,
caption = "#TidyTuesday:\nDataset 2019-02-12\nShreyas Ragavan"
)
climate_spend_plt_fn(climate_spend_dept_y,
y_range_low = 2011,
y_range_hi = 2017,
caption = "#TidyTuesday:\nDataset 2019-02-12\nShreyas Ragavan"
)