-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_legend_placement.qmd
More file actions
121 lines (92 loc) · 2.86 KB
/
Copy path7_legend_placement.qmd
File metadata and controls
121 lines (92 loc) · 2.86 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
---
title: "Placing legends"
author: "Michael Stadler, Charlotte Soneson"
format:
html:
toc: true
toc-location: left
embed-resources: true
link-external-icon: true
code-fold: true
editor_options:
chunk_output_type: console
---
## Summary
This document illustrates how to place legends in different positions in a
`ggplot`.
## Prepare data
Run the following code to load the data used in this document:
```{r}
#| label: prepare_data
#| code-fold: false
suppressPackageStartupMessages({
library(dplyr)
library(tibble)
library(swissknife)
library(ggplot2)
})
loadExampleData("mycars")
tibble(mycars)
```
## Step 1 - construct basic plot
```{r}
#| label: make_basic_plot
gg <- ggplot(mycars,
aes(x = mpg, y = disp, color = cyl)) +
geom_point(size = 4) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = "Miles/gallon", y = "Displacement") +
theme_bw(base_size = 13)
gg
```
## Step 2 - move legend to the bottom
```{r}
#| label: legend_bottom
gg + theme(legend.position = "bottom")
```
## Step 3 - move legend inside the canvas (top right corner)
```{r}
#| label: legend_inside
gg + theme(legend.position = "inside",
legend.position.inside = c(0.97, 0.97),
legend.justification = c(1, 1),
legend.background = element_rect(color = "black"),
legend.title.position = "left")
```
## Step 4 - hide specific legend
Use the `cyl` column for both color and shape, but only show the legend for
the shape.
```{r}
#| label: hide_one_legend
ggplot(mycars,
aes(x = mpg, y = disp, color = cyl, shape = cyl)) +
geom_point(size = 4) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = "Miles/gallon", y = "Displacement") +
theme_bw(base_size = 13) +
guides(color = "none",
shape = guide_legend(title = "Number of\ncylinders"))
```
## Step 5 - place different legends in different positions
```{r}
ggplot(mycars,
aes(x = mpg, y = disp, color = gear, shape = cyl)) +
geom_point(size = 4) +
scale_color_continuous(name = "Number of\ngears") +
scale_shape_discrete(name = "Number of\ncylinders") +
labs(x = "Miles/gallon", y = "Displacement") +
theme_bw(base_size = 13) +
guides(color = guide_colorbar(position = "right"),
shape = guide_legend(position = "bottom"))
```
## Links
* Legends in `ggplot2` 3.5.0: [https://www.tidyverse.org/blog/2024/02/ggplot2-3-5-0-legends/](https://www.tidyverse.org/blog/2024/02/ggplot2-3-5-0-legends/)
## Session info
```{r}
#| label: session_info
sessioninfo::session_info()
```