-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_workbook.Rmd
169 lines (130 loc) · 3.45 KB
/
03_workbook.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
---
title: "Grammar of Graphics"
author: "Aaron R. Williams"
output:
html_document:
code_folding: hide
toc: TRUE
toc_float: TRUE
editor_options:
chunk_output_type: console
---
```{r}
library(tidyverse)
theme_set(theme_minimal())
```
## Exercise 1
```{r}
tidy_anscombe <-
anscombe %>%
# make the wide data too long
pivot_longer(
cols = everything(),
names_to = "names",
values_to = "value"
) %>%
# split the axis and quartet id
mutate(
coord = str_sub(names, start = 1, end = 1),
quartet = str_sub(names, start = 2, end = 2)
) %>%
group_by(quartet, coord) %>%
mutate(id = row_number()) %>%
# make the data tidy
pivot_wider(id_cols = c(id, quartet), names_from = coord, values_from = value) %>%
ungroup() %>%
select(-id)
## YOUR CODE GOES HERE
ggplot(data = tidy_anscombe, mapping = aes(x = x, y = y)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
facet_wrap(~ quartet)
```
## Exercise 2
```{r}
titanic <- tribble(
~Class, ~Sex, ~n,
"1st class", "female passengers", 144,
"1st class", "male passengers", 179,
"2nd class", "female passengers", 106,
"2nd class", "male passengers", 171,
"3rd class", "female passengers", 216,
"3rd class", "male passengers", 493
)
## YOUR CODE GOES HERE
titanic %>%
ggplot(mapping = aes(x = Class, y = n, fill = Sex)) +
geom_col()
```
## Exercise 3
```{r}
## YOUR CODE GOES HERE
# bars are stacked
titanic %>%
ggplot(mapping = aes(x = Class, y = n, fill = Sex)) +
geom_col() +
labs(
title = "More Men than Women in Titanic deaths",
caption = "Data from library(titanic)"
)
```
## Exercise 4
```{r}
# install.packages("ggtext")
library(ggtext)
# YOUR WORK GOES HERE
# bars are next to each other
titanic %>%
ggplot(mapping = aes(x = Class, y = n, fill = Sex)) +
geom_col(position = "dodge") +
labs(
title = "More Men than Women in Titanic deaths",
caption = "Data from library(titanic)"
) +
theme(plot.title = element_markdown()) +
labs(
title = "More
<span style='color:#ADD8E6;'>male passengers</span> died than
<span style='color:#FFC0CB;'>female passengers</span> in all three classes",
x = NULL,
y = NULL
) +
guides(fill = "none")
```
## Exercise 5
```{r warning = FALSE}
library(gghighlight)
library(gapminder)
data <- gapminder %>%
filter(continent %in% c("Europe")) %>%
group_by(country) %>%
mutate(pcgdp_change = ifelse(year == 1952, 0, gdpPercap - lag(gdpPercap))) %>%
mutate(pcgdp_change = cumsum(pcgdp_change))
data |>
ggplot(mapping = aes(x = year, y = pcgdp_change, color = country)) +
geom_line() +
gghighlight(max(pcgdp_change) > 35000) +
scale_x_continuous(
expand = expansion(mult = c(0.002, 0)),
breaks = c(seq(1950, 2010, 10)),
limits = c(1950, 2010)
) +
scale_y_continuous(
expand = expansion(mult = c(0, 0.002)),
breaks = 0:8 * 5000,
labels = scales::dollar,
limits = c(0, 40000)
) +
labs(
x = "Year",
y = "Change in per-capita GDP (US dollars)"
)
# YOUR WORK GOES HERE
data <- gapminder %>%
filter(continent %in% c("Europe")) %>%
group_by(country) %>%
mutate(pcgdp_change =
ifelse(year = 1952, 0, gdpPercap - lag(gdpPercap))) %>%
mutate(pcgdp_change = cumsum(pcgdp_change)) +
gghighligh(max(pcgdp_change) > 35000)
```