-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09 - Arranging Plots.Rmd
85 lines (65 loc) · 1.48 KB
/
09 - Arranging Plots.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
---
title: "ggplot2 Elegant Graphics"
author: "Brandon Foltz"
date: "2023-03-13"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(patchwork)
```
```{r}
p1 <- ggplot(mpg) +
geom_point(aes(x = displ, y = hwy))
p2 <- ggplot(mpg) +
geom_bar(aes(x = as.character(year), fill = drv), position = "dodge") +
labs(x = "year")
p3 <- ggplot(mpg) +
geom_density(aes(x = hwy, fill = drv), colour = NA) +
facet_grid(rows = vars(drv))
p4 <- ggplot(mpg) +
stat_summary(aes(x = drv, y = hwy, fill = drv), geom = "col", fun.data = mean_se) +
stat_summary(aes(x = drv, y = hwy), geom = "errorbar", fun.data = mean_se, width = 0.5)
```
```{r}
p1 + p2
p1 + p2 + p3 + p4
p1 + p2 + p3 + plot_layout(ncol = 2)
p1 / p2
p1 | p2
p3 | (p2 / (p1 | p4))
layout <- "
AAB
C#B
CDD
"
p1 + p2 + p3 + p4 + plot_layout(design = layout)
```
```{r}
p1 + p2 + p3 + plot_layout(ncol = 2, guides = "collect")
p1 + p2 + p3 + guide_area() + plot_layout(ncol = 2, guides = "collect")
```
```{r}
p12 <- p1 + p2
p12[[2]] <- p12[[2]] + theme_light()
p12
```
```{r}
p1 + p4 & theme_minimal()
```
```{r}
p1 + p4 & scale_y_continuous(limits = c(0, 45))
```
```{r}
p34 <- p3 + p4 + plot_annotation(
title = "A closer look at the effect of drive train in cars",
caption = "Source: mpg dataset in ggplot2"
) +
plot_layout(guides = "collect")
p34
```
```{r}
p123 <- p1 | (p2 / p3)
p123 + plot_annotation(tag_levels = "I") # Uppercase roman numerics
```