-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm005 Worksheet- Exploring Geometric Objects.Rmd
202 lines (142 loc) · 5.31 KB
/
cm005 Worksheet- Exploring Geometric Objects.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
---
title: "cm005 Worksheet: Exploring Geometric Objects"
output:
html_document:
keep_md: true
theme: paper
---
## Preliminary
Begin by loading the required packages. If you don't have these installed (or don't know whether you have them installed), you can install them by executing the following code in your console:
```
install.packages("tidyverse")
install.packages("scales")
install.packages("tsibble")
```
Now run this code chunk to load the packages:
```{r load packages, warning = FALSE}
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(gapminder))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(tsibble))
knitr::opts_chunk$set(fig.align = "center")
```
<!---The following chunk allows errors when knitting--->
```{r allow errors, echo = FALSE}
knitr::opts_chunk$set(error = TRUE)
```
## Exercise 1: Bar Chart Grammar (Together)
Consider the following plot. Don't concern yourself with the code at this point.
```{r, fig.width = 5, fig.height = 2}
gapminder %>%
filter(year == 2007) %>%
mutate(continent = fct_infreq(continent)) %>%
ggplot(aes(continent)) +
geom_bar() +
theme_bw()
```
Fill in the seven grammar components for this plot.
| Grammar Component | Specification |
|-----------------------|---------------|
| __data__ | `gapminder` |
| __aesthetic mapping__ | X:continent, Y:count |
| __geometric object__ |bars |
| scale | linear |
| statistical transform | count |
| coordinate system | rectangular |
| facetting | no |
## Exercise 2: `ggplot2` Syntax (Your Turn)
The following is a tsibble (a special type of tibble containing time series data, which we'll see more of later), stored in the variable `mauna`, of CO$_2$ concentrations collected monthly at the Mauna Loa station.
Execute this code to store the data in `mauna`:
```{r}
(mauna <- tsibble::as_tsibble(co2) %>%
rename(month = index, conc = value))
```
### 2(a)
Produce a line chart showing the concentration over time. Specifically, the plot should have the following grammar components:
| Grammar Component | Specification |
|-----------------------|---------------|
| __data__ | `mauna` |
| __aesthetic mapping__ | x: month, y: conc |
| __geometric object__ | lines |
| scale | linear |
| statistical transform | none |
| coordinate system | rectangular |
| facetting | none |
Fill in the blanks to obtain the plot:
```{r, fig.width = 5, fig.height = 2}
ggplot(mauna, aes(month, conc)) +
geom_line
```
### 2(b)
It turns out that you're allowed to specify the aesthetic mappings in a `geom` layer instead of, or in addition to, in the `ggplot()` function, with the following rules:
- Aesthetics appearing in a `geom` layer apply only to that layer.
- If there are conflicting aesthetics in both the `ggplot()` function and the `geom` layer, the `geom` layer takes precedence.
The following code mistakenly puts the month variable on the y-axis. Fill in the `FILL_THIS_IN` so that you still obtain the same result as above.
```{r, fig.width = 5, fig.height = 2}
ggplot(mauna, aes(y = month)) +
geom_line(aes(x=month,y=conc))+ ylab("CO2")
```
### 2(c)
You can store the output of the plot in a variable, too. Store the plot from 2(a) in the variable named `p`, then add a layer to `p` that adds green points to the plot.
```{r, fig.width = 5, fig.height = 2}
p <- ggplot(mauna, aes(y = month)) +
geom_line(aes(x=month,y=conc))+ ylab("CO2")
```
```{r}
p +
geom_point(aes(x = month, y = conc ), colour = "green")
```
### 2(d)
What's wrong with the following code? Fix it.
```{r, fig.width = 5, fig.height = 2}
ggplot(data = gapminder) +
geom_point(aes(x = gdpPercap, y = lifeExp), alpha = 0.1)
## need to include aes(x=) instead of just x=
```
### 2(e) BONUS
So you're a ggplot2 pro? Then, let's see this plot adapted to polar coordinates. Specifically:
- angle is month (January through December)
- radius is CO$_2$ concentration
The plot should look like a spiral, or concentric circles.
```{r, fig.width = 5, fig.height = 2}
FILL_THIS_IN
```
## Exercise 3: Fix the plots (Together)
### 3(a)
Fix the following plot (attribution: ["R for data science"](https://r4ds.had.co.nz/data-visualisation.html)).
```{r, fig.width = 5, fig.height = 2}
ggplot(mpg, aes(cty, hwy)) +
geom_point()
```
### 3(b)
Fix this plot so that it shows life expectancy over time _for each country_.
```{r, fig.width = 5, fig.height = 2}
ggplot(gapminder, aes(year, lifeExp)) +
geom_line()
```
### 3(c)
The following mock data set marks the (x,y) position of a caribou at four time points. Fix the plot below so that it shows the path of the caribou. Add an arrow with `arrow = arrow()`.
```{r, fig.width = 5, fig.height = 2}
motion <- tribble(
~time, ~x, ~y,
1, 0.3, 0.3,
2, 0.8, 0.7,
3, 0.5, 0.9,
4, 0.4, 0.5
)
ggplot(motion, aes(x, y)) +
geom_line()
```
### 3(d)
The following plot attempts to put both the raw data and boxplots together, but it's hard to see the raw data. Fix the plot.
```{r, fig.width = 5, fig.height = 2}
ggplot(gapminder, aes(continent, lifeExp)) +
geom_point() +
geom_boxplot()
```
### 3(e)
Change the following plot so that it shows _proportion_ on the y-axis, not count.
```{r, fig.width = 5, fig.height = 2}
ggplot(mtcars, aes(cyl)) +
geom_bar()
```