-
Notifications
You must be signed in to change notification settings - Fork 105
/
ggplot2.qmd
executable file
·67 lines (49 loc) · 1.73 KB
/
ggplot2.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
62
63
64
65
66
67
# ggplot2
## Introduction
Styling suggestions for `+` used to separate ggplot2 layers are very similar to those for `|>` in pipelines.
## Whitespace
`+` should always have a space before it, and should be followed by a new line. This is true even if your plot has only two layers. After the first step, each line should be indented by two spaces.
If you are creating a ggplot off of a dplyr pipeline, there should only be one level of indentation.
```{r}
# Good
iris |>
filter(Species == "setosa") |>
ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point()
# Bad
iris |>
filter(Species == "setosa") |>
ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point()
# Bad
iris |>
filter(Species == "setosa") |>
ggplot(aes(x = Sepal.Width, y = Sepal.Length)) + geom_point()
```
## Long lines
If the arguments to a ggplot2 layer don't all fit on one line, put each argument on its own line and indent:
```{r}
# Good
ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point() +
labs(
x = "Sepal width, in cm",
y = "Sepal length, in cm",
title = "Sepal length vs. width of irises"
)
# Bad
ggplot(aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_point() +
labs(x = "Sepal width, in cm", y = "Sepal length, in cm", title = "Sepal length vs. width of irises")
```
ggplot2 allows you to do data manipulation, such as filtering or slicing, within the `data` argument. Avoid this, and instead do the data manipulation in a pipeline before starting plotting.
```{r}
# Good
iris |>
filter(Species == "setosa") |>
ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point()
# Bad
ggplot(filter(iris, Species == "setosa"), aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point()
```