-
Notifications
You must be signed in to change notification settings - Fork 1
/
ch-10.Rmd
104 lines (66 loc) · 3.37 KB
/
ch-10.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
---
title: "Solutions to Chapter 10 Exercises"
author: "Howard Baek"
date: "Last compiled on `r format(Sys.time(), '%B %d, %Y')`"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, include=FALSE}
library(tidyverse)
```
1. The following code creates two plots of the mpg dataset. Modify the code so that the legend and axes match, without using faceting!
```{r}
fwd <- subset(mpg, drv == "f")
rwd <- subset(mpg, drv == "r")
ggplot(fwd, aes(displ, hwy, colour = class)) +
geom_point() +
scale_color_discrete(limits = c("compact", "midsize", "minivan", "subcompact",
"2seater", "suv")) +
coord_cartesian(xlim = c(1, 8),
ylim = c(15, 50))
ggplot(rwd, aes(displ, hwy, colour = class)) +
geom_point() +
scale_color_discrete(limits = c("compact", "midsize", "minivan", "subcompact",
"2seater", "suv")) +
coord_cartesian(xlim = c(1, 8),
ylim = c(15, 50))
```
- We can make the legend and axes match by manually setting the limits of color.
2. What happens if you add two `xlim()` calls to the same plot? Why?
```{r}
ggplot(fwd, aes(displ, hwy, colour = class)) +
geom_point() +
xlim(2, 5) +
xlim(3, 4)
```
- You get a very informative message: `Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.` I'm guessing the reason is ggplot evaluates the code top-down. Since the second `xlim` call happens after the first `xlim`, it replaces the first `xlim`.
3. What does `scale_x_continuous(limits = c(NA, NA))` do?
```{r}
ggplot(fwd, aes(displ, hwy, colour = class)) +
geom_point() +
scale_x_continuous(limits = c(NA, NA))
```
- It doesn't change the limits because, according to the help page, "use NA to refer to the existing minimum or maximum". This means that we are setting the limits to be from the existing minimum to the existing maximum.
4. What does `expand_limits()` do and how does it work? Read the source code.
```{r, eval = FALSE}
?expand_limits
```
- According to the help page, "it ensures limits include a single value, for all panels or all plots". It is a wrapper around `geom_blank()` and makes it easy to add such values.
### 10.1.8 Exercises
1. Recreate the following graphic:
```{r}
ggplot(mpg, aes(displ, hwy)) +
geom_point(size = 3) +
scale_x_continuous("Displacement", labels = scales::unit_format(suffix = "L")) +
scale_y_continuous(quote(paste("Highway ", (frac(miles, gallon)))))
```
2. List the three different types of object you can supply to the `breaks` argument. How do `breaks` and `labels` differ?
According to the help page, you can supply NULL, transformation object (from `waiver()`), a numeric vector, and a function that takes limits as input and returns breaks as output.
- The `labels` argument takes in a character vector instead of a numeric vector, which `breaks` accepts. Also, it accepts a function that takes breaks as input (instead of limits) and returns labels as output (instead of breaks).
3. What label function allows you to create mathematical expressions? What label function converts 1 to 1st, 2 to 2nd, and so on?
- `label_math()` allows you to create mathematical expressions.
- `label_ordinal()` allows you to label ordinal numbers (1 to 1st, 2 to 2nd, 3 to 3rd and so on)