Repository navigation
Expand file tree
/
Copy pathfaceting.qmd
More file actions
126 lines (95 loc) · 3.27 KB
/
Copy pathfaceting.qmd
File metadata and controls
126 lines (95 loc) · 3.27 KB
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
# Faceting
In this chapter, we will introduce faceting, a technique that produces multiple plots in which the data is subsetted by a categorical feature.
## Faceting on one variable: `facet_wrap()`
```{r}
#| echo: false
library(tidyverse)
```
```{r}
#| fig-width: 5
#| fig-height: 3
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(color = "cornflowerblue") +
facet_wrap(~Species)
```
To control the layout dimensions, you can choose the number of rows or columns (`nrow =` or `ncol =`).
## Reordering facets
Facets are ordered according to the factor level order of the faceted variable. See [the chapter on factors](factors.hmtl) for strategies on reordering factor levels.
```{r}
library(MASS)
painters |>
rownames_to_column("Name") |>
filter(School == "A") |>
pivot_longer(Composition:Expression, names_to = "Skill",
values_to = "Score") |>
ggplot(aes(x = Score, y = fct_reorder(Name, Score))) +
geom_point(color= "#786FB8") +
facet_wrap(~Skill, ncol = 1)
```
```{r}
birds_plot <- birds |>
group_by(phase_of_flt) |>
summarize(count = n()) |>
slice_max(order_by = count, n = 4)
ggplot(birds, aes(x = speed, y = after_stat(density))) +
geom_histogram() +
facet_wrap(~phase_of_flt, nrow = 1)
```
## Faceting on two variables: `facet_grid()`
With two variables, the rows represent the levels of one variable and the columns the other. These can be specified with the formula notation:
`facet_wrap(`*row variable*` ~ `*column variable*`)`.
```{r}
library(scales)
library(openintro)
ggplot(cle_sac, aes(x = age, y = personal_income)) +
geom_point(size = 1, color = "cornflowerblue") +
facet_grid(sex ~ city) +
scale_y_continuous(labels = unit_format(unit = "K", scale = .001)) +
labs(x = "age (in years)", y = "personal income",
caption = "Data: openintro::cle_sac") +
theme_bw(16)
```
## Faceting with categorical variables
```{r}
#| fig-width: 10
#| fig-height: 6
#| out-width: 90```
library(pgmm)
data(wine)
tidywine <- wine |>
pivot_longer(cols = -Type, names_to = "variable",
values_to = "value")
tidywine |>
ggplot(aes(value)) +
geom_histogram(color = "blue", fill = "cornflowerblue",
bins = 20) +
facet_wrap(~variable) +
labs(title = "Fixed scales (default)") +
theme_grey(14)
```
Axis scales can be made independent, by setting scales to `free`, `free_x`, or `free_y`.
In this case, `scales = "free_x"` is a better option because the distribution of each numerical variable is more obvious.
```{r}
#| fig-width: 10
#| fig-height: 6
#| out-width: 90```
tidywine |>
ggplot(aes(value)) +
geom_histogram(color = "blue", fill = "cornflowerblue",
bins = 20) +
facet_wrap(~variable, scales = "free_x") +
labs(title = "Free x scale") +
theme_grey(14)
```
## Faceting on two variables
`facet_grid` can be used to split data-sets on two variables and plot them on the horizontal and/or vertical direction.
```{r,fig.width=10, fig.height=6}
wine |>
mutate(Type = paste("Type", Type)) |>
select(1:6) |>
pivot_longer(cols = -Type, names_to = "variable", values_to = "value") |>
ggplot(aes(value)) +
geom_histogram(color = mycol, fill = "lightblue") +
facet_grid(Type ~ variable, scales = "free_x") +
theme_grey(14)
```