-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscript_2020-02-18.R
66 lines (49 loc) · 2.14 KB
/
script_2020-02-18.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
library(countrycode)
library(ggbeeswarm)
library(tidyverse)
library(gganimate)
#### Data ####
food_consumption <- read_csv("data/data_2020-02-18.csv", col_types = "ccdd")
olympic_colors <- c(Oceania = "#00A651", Africa = "#000000", Asia = "#FCB131",
Europe = "#0081C8", Americas = "#EE334E")
#### Table ####
df <-
food_consumption %>%
mutate(continent = countrycode(country, origin = "country.name",
destination = "continent"),
food_category = str_to_lower(food_category),
food_category = case_when(
food_category == "wheat and wheat products" ~ "wheat",
food_category == "milk - inc. cheese" ~ "milk & cheese",
food_category == "nuts inc. peanut butter" ~ "nuts",
TRUE ~ food_category),
food_category = fct_reorder(food_category, consumption))
#### Plot ####
p <-
ggplot(df) +
aes(x = continent, y = consumption) +
geom_boxplot(aes(fill = continent, color = continent), alpha = 0.7) +
geom_quasirandom(aes(color = continent)) +
scale_fill_manual(values = olympic_colors) +
scale_color_manual(values = olympic_colors) +
scale_y_continuous(sec.axis = dup_axis()) +
labs(title = "Average consumption of {closest_state} per country",
x = NULL, y = "In kilogram per person per year",
caption = "Source: nu3.de\n@_abichat for #TidyTuesday\n") +
theme_bw() +
theme(legend.position = "none",
text = element_text(size = 20, family = "EconSansCndReg"),
axis.title.y.left = element_blank(),
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
plot.margin = unit(c(10, 10, 10, 10), units = "points"),
plot.background = element_rect(fill = "#f4f4e9"),
panel.background = element_rect(fill = "#f4f4e9"),
panel.grid.major = element_line(color = "#dbdbb6"),
panel.grid.minor = element_blank())
a <-
p +
transition_states(food_category, state_length = 2) +
view_zoom(step_length = 2, nsteps = 11)
animate(a, nframes = 300, width = 29, height = 21, units = "cm", res = 320)
anim_save("plots/plot_2020-02-18.gif")