-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path17-faceting.Rmd
68 lines (47 loc) · 1.77 KB
/
17-faceting.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
# Faceting
```{r, include=FALSE}
library(tidyverse)
```
## Exercises
**1.**
```{r}
# faceting by cut and grouping by carat.
diamonds %>%
ggplot(aes(price)) +
geom_histogram(aes(color = carat)) +
facet_wrap(~cut, scales = "free_y")
# faceting by carat and grouping by cut.
diamonds %>%
ggplot(aes(price)) +
geom_histogram(aes(color = cut)) +
facet_wrap(~carat, scales = "free_y")
```
- It makes more sense to facet by cut because its a discrete variable. Faceting by carat, a continuous variable, makes too many facets and renders the plot unreadable!
<br>
**2.**
```{r}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point(aes(color = color))
```
```{r}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point(aes(color = color)) +
facet_wrap(~color)
```
- I think its better to use grouping to compare the different colors. The panels all have the same shape, so it's hard to compare the groups across facets. If I use faceting, I'd add that the plot is facetted by diamond colour, from D (best) to J (worst).
<br>
**3.** I think `facet_wrap()` is more useful than `facet_grid()` because the former function is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. In data analysis, its extremely common to have a single variable with many levels that the analyst wants to arrange the for easy comparison. Although `facet_grid()` works on single variables, `facet_wrap()` involves less typing when you have a single variable.
<br>
**4.**
```{r}
mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f") & class != "2seater")
mpg2 %>%
ggplot(aes(displ, hwy)) +
geom_point() +
geom_smooth(data = mpg2 %>% select(-class),
se = FALSE,
method = "loess") +
facet_wrap(~class)
```