Skip to content

Commit

Permalink
add facet examples (#5725)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Mar 18, 2024
1 parent 85bd22c commit 25258f5
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions vignettes/ggplot2-in-packages.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ To create any graphic using ggplot2 you will probably need to use `aes()` at lea
```{r}
mpg_drv_summary <- function() {
ggplot(ggplot2::mpg) +
geom_bar(aes(x = drv)) +
coord_flip()
geom_bar(aes(y = drv)) +
facet_wrap(vars(year))
}
```

Expand All @@ -81,33 +81,33 @@ If you already know the mapping in advance (like the above example) you should u
```{r}
mpg_drv_summary <- function() {
ggplot(ggplot2::mpg) +
geom_bar(aes(x = .data$drv)) +
coord_flip()
geom_bar(aes(y = .data$drv)) +
facet_wrap(vars(.data$year))
}
```

If you have the column name as a character vector (e.g., `col = "drv"`), use `.data[[col]]`:

```{r}
col_summary <- function(df, col) {
col_summary <- function(df, col, by) {
ggplot(df) +
geom_bar(aes(x = .data[[col]])) +
coord_flip()
geom_bar(aes(y = .data[[col]])) +
facet_wrap(vars(.data[[by]]))
}
col_summary(mpg, "drv")
col_summary(mpg, "drv", "year")
```

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()`.

```{r, eval = (packageVersion("rlang") >= "0.3.4.9003")}
col_summary <- function(df, col) {
col_summary <- function(df, col, by) {
ggplot(df) +
geom_bar(aes(x = {{ col }})) +
coord_flip()
geom_bar(aes(y = {{ col }})) +
facet_wrap(vars({{ by }}))
}
col_summary(mpg, drv)
col_summary(mpg, drv, year)
```

To summarise:
Expand Down

0 comments on commit 25258f5

Please sign in to comment.