You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/ggplot2-in-packages.Rmd
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -58,8 +58,8 @@ To create any graphic using ggplot2 you will probably need to use `aes()` at lea
58
58
```{r}
59
59
mpg_drv_summary <- function() {
60
60
ggplot(ggplot2::mpg) +
61
-
geom_bar(aes(x = drv)) +
62
-
coord_flip()
61
+
geom_bar(aes(y = drv)) +
62
+
facet_wrap(vars(year))
63
63
}
64
64
```
65
65
@@ -81,33 +81,33 @@ If you already know the mapping in advance (like the above example) you should u
81
81
```{r}
82
82
mpg_drv_summary <- function() {
83
83
ggplot(ggplot2::mpg) +
84
-
geom_bar(aes(x = .data$drv)) +
85
-
coord_flip()
84
+
geom_bar(aes(y = .data$drv)) +
85
+
facet_wrap(vars(.data$year))
86
86
}
87
87
```
88
88
89
89
If you have the column name as a character vector (e.g., `col = "drv"`), use `.data[[col]]`:
90
90
91
91
```{r}
92
-
col_summary <- function(df, col) {
92
+
col_summary <- function(df, col, by) {
93
93
ggplot(df) +
94
-
geom_bar(aes(x = .data[[col]])) +
95
-
coord_flip()
94
+
geom_bar(aes(y = .data[[col]])) +
95
+
facet_wrap(vars(.data[[by]]))
96
96
}
97
97
98
-
col_summary(mpg, "drv")
98
+
col_summary(mpg, "drv", "year")
99
99
```
100
100
101
101
If the column name or expression is supplied by the user, you can also pass it to `aes()` or `vars()` using `{{ col }}`. This tidy eval operator captures the expression supplied by the user and forwards it to another tidy eval-enabled function such as `aes()` or `vars()`.
0 commit comments