-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeer_states.Rmd
211 lines (197 loc) · 6.13 KB
/
Beer_states.Rmd
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
title: "Untitled"
author: "Tu"
date: "6/22/2020"
output: html_document
---
```{r include=FALSE}
library(tidyverse)
library(geofacet)
library(ggtext)
library(showtext)
library(ggchicklet)
font_add_google("Indie Flower", "Indie Flower")
theme_set(theme_void(base_family = "Indie Flower", base_size = 15))
showtext_auto()
```
```{r}
beer_plot <- beer %>%
mutate(
people_drink = consume_per_capita * adults_pop,
deficiency = if_else((people_drink - production_sum) > 0, people_drink - production_sum, 0)
) %>%
pivot_longer(
cols = c(deficiency, production_sum),
names_to = "type",
values_to = "amount"
) %>%
mutate(
uniform_col = "THIS",
type = factor(type, levels = c("deficiency", "production_sum"))
)
```
```{r}
set.seed(222)
beer_dot <- beer %>%
mutate(
people_drink = consume_per_capita * adults_pop,
deficiency = people_drink - production_sum
) %>%
group_by(state) %>%
slice(rep(1, each = floor(consume_per_capita))) %>%
transmute(
state = factor(state),
uniform_col = "THIS",
dots_brewer = runif(floor(consume_per_capita),
min = if_else(production_sum > people_drink, people_drink * 0.1, production_sum * 0.1),
max = if_else(production_sum < people_drink, people_drink * 0.9, production_sum * 0.9)
),
dot_size = runif(floor(consume_per_capita), 0, 1)
) %>%
ungroup()
```
## America
```{r}
US <- beer %>%
transmute(
US_prodution = sum(production_sum),
overflow = sum(production_sum) - sum(consume_per_capita * adults_pop)
) %>%
pivot_longer(
cols = c(overflow, US_prodution),
names_to = "type",
values_to = "amount"
) %>%
mutate(
uniform_col = "THIS",
type = factor(type, levels = c("overflow", "US_prodution"))
) %>%
distinct()
US_dot <- US %>%
slice(rep(1, each = 12)) %>%
mutate(
dots_brewer = runif(12,
min = 2330310745 * 0.1,
max = 5173564192 * 0.9
),
dot_size = runif(12, 0, 1)
)
```
```{r fig.height=3, fig.width=3}
# US_p <-
US %>%
ggplot() +
geom_chicklet(aes(x = uniform_col, y = amount, fill = fct_rev(type))) +
scale_fill_manual(values = c("#F2CF77", "#c96e12")) +
geom_jitter(
data = US_dot,
aes(uniform_col, dots_brewer, size = dot_size),
position = position_jitter(width = 0.5, height = 0.22, seed = 3),
shape = 21,
color = colorspace::darken("#f9e556", .45),
stroke = .3,
# colorspace::darken("#f9e556", .45)
alpha = 0.5
) +
theme(legend.position = "none")
```
## geom_col
```{r fig.height=13.5, fig.width=22}
#p <-
beer_plot %>%
full_join(us_state_grid2, by = c("state" = "code")) %>%
ggplot(aes(1000, 1000)) +
geom_chicklet(aes(x = uniform_col, y = amount, fill = fct_rev(type))) +
scale_fill_manual(values = c("#F2CF77", "white")) +
# f4d380/F2CF77
# bubbles
geom_jitter(
data = beer_dot,
aes(uniform_col, dots_brewer, size = dot_size),
position = position_jitter(width = 0.5, height = 0.3, seed = 2),
shape = 21,
color = colorspace::darken("#f9e556", .45),
stroke = .3,
# colorspace::darken("#f9e556", .45)
alpha = 0.6
) +
geom_text(
data = beer_plot,
mapping = aes(x = 0.6, y = -Inf, label = state),
family = "Indie Flower",
hjust = 0.01,
vjust = -1,
colour = "#1C1259",
fontface = "bold",
size = 8
) +
facet_geo(vars(state), grid = "us_state_grid2", scales = "free_y") +
labs(
title = "How much <span style='color:white'>beer would be needed</span> if legal drinkers in each state <br>were only allowed to drink <span style='color:#F2CF77'>local beer</span> in 2018?",
subtitle = "Only 1/3 states' <span style='color:#F2CF77'>total beer production</span> in 2018 can fulfill the local beer crave, but US in total produces more than 30% more <br> beer than US adult citizens drink in 2018.<br><span font-size:15em> Yellow bar is the <span style='color:#F2CF77'>total beer production</span>in that state, and the white bar is the <span style='color:white'>beer in demand</span>.<br>Number of bubbles represents the number of gallons consumed per person in each states. </span>",
caption = "Source:Datafrom the Alcohol and Tobacco Tax and Trade Bureau (TTB) and National Institute on Alcohol Abuse and Alcoholism (NIAA)"
# https://pubs.niaaa.nih.gov/publications/surveillance115/pcyr1970-2018.txt"
) +
theme(
plot.background = element_rect(fill = "#48a4d6"),
# 1C1259
legend.position = "none",
plot.title = element_markdown(
face = "bold",
size = rel(2.8),
color = "#1C1259",
hjust = 0.02,
margin = margin(t = 6, b = 7)
),
plot.subtitle = element_markdown(
size = rel(1.6),
color = "#1C1259",
# hjust = 0.06,
margin = margin(t = 5, b = 3)
),
plot.caption = element_text(
face = "italic",
# size = rel(1.1),
color = "#1C1259",
hjust = 0.47
),
strip.text = element_blank()
) +
coord_cartesian(clip = "off")
```
## geom_rect
```{r fig.height=12, fig.width=18}
beer %>%
# mutate(state = factor(state)) %>%
full_join(us_state_grid2, by = c("state" = "code")) %>%
ggplot() +
geom_rect(aes(xmin = state, xmax = production_sum / 10e7, ymin = 0, ymax = people_drink), fill = "#EBE1CE") +
geom_rect(aes(xmin = state, xmax = production_sum / 10e7, ymin = 0, ymax = production_sum), fill = "#f9e556") +
# bubbles
geom_jitter(
data = beer_dot,
aes(state, dots_brewer, size = dot_size),
position = position_jitter(width = 0.7),
shape = 21,
color = colorspace::darken("#f9e556", .17),
stroke = .3,
) +
facet_geo(vars(state), grid = "us_state_grid2", label = "code", scales = "free_y") +
theme_void() +
theme(
plot.background = element_rect(fill = "#1C1259"),
strip.text = element_text(hjust = 0.3, vjust = 0.8),
legend.position = "none",
plot.caption = element_text(face = "italic")
) +
labs(
subtitle = "North Dakota and South Dakota need the least number of people to consume their local beer.",
caption = "Source:
Data is downloaded from NIAA https://pubs.niaaa.nih.gov/publications/surveillance115/pcyr1970-2018.txt"
)
```
```{r}
ggsave("p.pdf", p,
width = 22, height = 13.5, units = "in", device = cairo_pdf
)
```