Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add facet examples to programming vignette #5725

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the bidirectional geom feature here instead of deprecated 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
Loading