-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm007.Rmd
195 lines (140 loc) · 6.53 KB
/
cm007.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
---
title: "cm007 Notes and Exercises: ggplot2, Round 2"
date: '2017-09-26'
output:
html_document:
toc: true
---
```{r}
suppressPackageStartupMessages(library(tidyverse)) # The tidyverse contains ggplot2!
suppressPackageStartupMessages(library(gapminder))
knitr::opts_chunk$set(fig.width=4, fig.height=3)
```
## Continuation of Scatterplots
### Regression curve
- Crash course on regression
- Regression analysis fits some curve through the data, representing the mean of Y given the specified X value.
- Sometimes we assume it's a line, and take this "mean curve" to be the line of best fit.
- Sometimes we fit a generic curve by averaging "nearby" points.
To add a regression line/curve, add a layer `geom_smooth` to your plot. Probably two of the most useful arguments to `geom_smooth` are:
- `method=`...
- ...`"lm"` for a straight line. Stands for "Linear Model".
- ...other for a generic curve (called a smoother; default, hence the "smooth" part of `geom_smooth`).
- `se=`... controls whether or not a confidence interval is plotted.
Examples:
```{r}
vc1 <- ggplot(gapminder, aes(year, lifeExp)) +
geom_point()
vc1 + geom_smooth(se=FALSE)
vc1 + geom_smooth(method="lm")
```
__Exercise 1__: Make a plot of `year` (x) vs `lifeExp` (y), with points coloured by continent. Then, to that same plot, fit a straight regression line to each continent, without the error bars. If you can, try piping the data frame into the `ggplot` function.
```{r}
gapminder %>%
ggplot(aes(x=year,y=lifeExp,color=continent))+
geom_point()+
geom_smooth(method="lm", se = FALSE)
```
__Exercise 2__: Repeat Exercise 1, but switch the _regression line_ and _geom\_point_ layers. How is this plot different from that of Exercise 1?
```{r}
gapminder %>%
ggplot(aes(x=year,y=lifeExp,color=continent))+
geom_smooth(method="lm", se = FALSE)+
geom_point()
```
__Exercise 3__: Omit the `geom_point` layer from either of the above two plots (it doesn't matter which). Does the line still show up, even though the data aren't shown? Why or why not?
```{r}
gapminder %>%
ggplot(aes(x=year,y=lifeExp,color=continent))+
geom_smooth(method="lm", se = FALSE)
```
### Facetting
We saw that we can __group__ by using scales. For example, we can distinguish continents by using different shape or colour:
```{r}
ggplot(gapminder, aes(gdpPercap, lifeExp)) +
geom_point(aes(colour=continent))
ggplot(gapminder, aes(gdpPercap, lifeExp)) +
geom_point(aes(shape=continent))
```
But these plots can get overloaded. In comes __facetting__ to save the day! Let's add this to our list of concepts:
- :white_check_mark: __geometric objects__, or `geom_`s.
- :white_check_mark: __scales__, linked by...
- :white_check_mark: __aesthetics__, through the `aes` function.
- \*NEW* __facetting__.
Facetting separates data from each group into its own "mini plot", called a _panel_. These panels are arranged next to each otherfor easier comparison. There are two facetting functions in `ggplot2`:
- `facet_wrap`: 1D facetting -- we'll focus on this first.
- `facet_grid`: 2D facetting
`facet_wrap` puts the panels in "reading order", and goes to a new line if there's not enough room. Mandatory argument specification is `facet_wrap(~ VARIABLE)`. Example:
```{r, fig.width=8}
ggplot(gapminder, aes(gdpPercap, lifeExp)) +
facet_wrap(~ continent) +
geom_point()
```
As for other arguments of `facet_wrap` that I find to be most useful, check the documentation for `scales` and `ncol` -- and if you're brave, `labeller`.
__Exercise 4__: Make a plot of `year` (x) vs `lifeExp` (y), facetted by continent. Then, fit a smoother through the data for each continent, without the error bars. Choose a span that you feel is appropriate.
```{r}
ggplot(gapminder, aes(year, lifeExp)) +
facet_wrap(~ continent) +
geom_point(alpha=.25,color="orange")+
geom_smooth(se=FALSE,span=1)
```
`facet_grid` puts the panels in a grid. Each row corresponds to one grouping, and each column corresponds to another grouping. Mandatory argument specification: `facet_grid(GROUPING_VARIABLE_1 ~ GROUPING_VARIABLE_2)`.
Example: Let's also facet by "small" (<=7,000,000 population) and "large" (>7,000,000 population) countries. We'll need to add a "size" variable; we'll do that with `dplyr`, and pipe the result into the `ggplot` function:
```{r, fig.width=8}
vc2 <- gapminder %>%
mutate(size=c("small", "large")[(pop>7000000) + 1]) %>%
ggplot(aes(gdpPercap, lifeExp)) +
facet_grid(size ~ continent)
vc2 + geom_point()
```
Everything we've learned prior to this works in conjunction with facetting:
- Colours:
```{r, fig.width=8}
vc2 + geom_point(aes(colour=year))
```
- Regression curves and log scales:
```{r, fig.width=8}
vc2 +
geom_point(colour="brown",
alpha=0.2) +
geom_smooth() +
scale_x_log10()
```
### Connect the dots with `geom_line`
Sometimes it makes sense to "connect the dots" in a scatterplot, especially if time is involved. The two functions to help us do this are:
- `geom_line`: connect the dots from left-to-right.
- `geom_path`: connect the dots in the order that they appear in the data frame.
With these `geom`s, it's so important to remember to specify `group=VARIABLE` in your aesthetics (`aes` function), otherwise ggplot won't distinguish between groups.
Example: life expectancy over time for each country.
```{r}
## Without the `group` specification:
ggplot(gapminder, aes(year, lifeExp)) +
geom_line()
## With the group specification:
ggplot(gapminder, aes(year, lifeExp, group=country)) +
geom_line(alpha=0.2)
```
PS: such "spaghetti plots" _are_ actually useful -- they give us a sense of the _distribution_ of trends.
`geom_path` is typically used when a "time" variable is not shown on an axis. For example, let's look at a scatterplot of `pop` vs. `gdpPercap` of Afghanistan, and let's "connect the dots" in the order of time.
```{r}
gapminder %>%
filter(country=="Afghanistan") %>%
arrange(year) %>%
ggplot(aes(pop, gdpPercap)) +
geom_point() +
geom_path()
```
We can see the _path_ that the population and GDP per capita took for Afghanistan.
__Exercise 5__: Plot the population over time (year) using lines, so that each country has its own line. Colour by `gdpPercap`. Add alpha transparency to your liking.
```{r}
gapminder %>%
ggplot(aes(year,pop,group=country))+
geom_line(alpha=.25,aes(color=gdpPercap))
```
__Exercise 6__: Add points to the plot in Exercise 5.
```{r}
gapminder %>%
ggplot(aes(year,pop,group=country))+
geom_line(alpha=.2,aes(color=gdpPercap))+
geom_point(color="orange")
```