-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path2020_09_15_USKidsSpending.R
84 lines (79 loc) · 2.55 KB
/
2020_09_15_USKidsSpending.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
library(tidytuesdayR)
library(tidyverse)
tuesdata <- tidytuesdayR::tt_load("2020-09-15") %>% .$kids
plot_data = tuesdata %>%
filter(year == max(year), variable == "PK12ed") %>%
group_by(state) %>%
summarise(val = sum(inf_adj),
ch = sum(inf_adj_perchild)) %>%
left_join(geofacet::us_state_grid1 %>% rename(state = name))
avg = plot_data %>%
summarise(val = mean(val),
ch = mean(ch))
ggplot(plot_data) +
aes(val, ch, color = ch * val) +
geom_hline(data = avg,
aes(yintercept = ch),
color = "#f07167",
size = 1) +
geom_vline(data = avg,
aes(xintercept = val),
color = "#0081a7",
size = 1) +
geom_point(size = 2) +
ggrepel::geom_text_repel(data = filter(
plot_data,
ch != max(ch) & ch != min(ch) & val != max(val) & val != min(val)
),
aes(label = code)) +
ggrepel::geom_label_repel(data = filter(
plot_data,
ch == max(ch) | ch == min(ch) | val == max(val) | val == min(val)
),
aes(label = state)) +
annotate(
na.rm = T,
geom = "text",
x = 70000000,
y = avg$ch + .3,
color = "grey20",
hjust = 1,
label = paste0("Mean Spend per Child = $", round(avg$ch, 2))
) +
annotate(
na.rm = T,
geom = "text",
x = avg$val - 600000,
y = 20,
color = "grey20",
hjust = 1,
angle = 90,
label = paste0("Mean Total Spend = $", round(avg$val, -4))
) +
scale_x_log10(labels = scales::label_dollar()) +
scale_y_log10(limits = c(4, 20), labels = scales::label_dollar()) +
scale_color_gradient(trans = scales::log10_trans(),
low = "#f07167",
high = "#0081a7") +
theme_minimal() +
theme(
legend.position = "none",
axis.line = element_line(),
aspect.ratio = .6,
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey98", color = NA),
plot.margin = unit(c(1, 1, 1, 1), "cm"),
plot.title = element_text(face = "bold", size = 20),
plot.caption = ggtext::element_markdown(
hjust = 1,
vjust = 1,
color = "grey20"
)
) +
labs(
y = "Public Spending on Children per Child\n",
x = "\nTotal Public Spending on Children",
title = "2016 US Spending on Kids",
subtitle = "Each dot corresponds to one state. The state with the greatest and lowest total and per-child spending are labelled.\n",
caption = "<br>Data from <b>{tidykids}</b> (jrosen48.github.io/tidykids)<br>Visualisation by <b>Jack Davison</b> (Twitter @JDavison_) | Code found at <b>github.com/jack-davison</b>"
)