generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfaceting.qmd
62 lines (49 loc) · 1.85 KB
/
faceting.qmd
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
# Faceting
In this chapter, we will introduce facets, which are usually used to combine continuous and categorical data.
## Faceting on one variable
Facet partitions a plot into a matrix of panels. Each panel shows a different subset of the data. By default, ``facet_wrap`` gives consistent scales, which is easier for comparison between different panels.
```{r,fig.width=4, fig.height=3}
library(ggplot2)
mycol = "#7192E3"
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(color = mycol) +
facet_wrap(~Species) +
theme_grey(18)
```
Rather than faceting on factor level, we can have one panel for each numerical variable.
```{r,fig.width=10, fig.height=6}
library(pgmm)
library(dplyr)
library(tidyr)
data(wine)
tidywine <- wine |>
pivot_longer(cols = -Type, names_to = "variable", values_to = "value")
tidywine |>
ggplot(aes(value)) +
geom_histogram() +
facet_wrap(~variable) +
ggtitle("Consistent scales") +
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}
tidywine |>
ggplot(aes(value)) +
geom_histogram() +
facet_wrap(~variable,scales = "free_x") +
ggtitle("Consistent scales") +
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)
```