`purrr::pmap()` output incompatible with `ggplot::aes()`

73 views
Skip to first unread message

Hossam Ghorab

unread,
Dec 1, 2021, 10:12:22 PM12/1/21
to ggplot2

The following *reprex* boils down to a single question, **is there anyway we can use the quoted variable names inside `ggplot2::aes()` instead of the plain text names**? Example: we typically use `ggplot(mpg, aes(displ, cyl))` , how to make `aes()` work normally with `ggplot(mpg, aes("displ", "cyl"))` ? 

If you understood my question, the remainder of this *reprex* really adds no information. However, I added it to draw the full picture of the problem.

***

More details: I want to use *purrr* functions to create a bunch of routinely exploratory data analysis plots effortlessly. The problem is, `purrr::pmap()` results the string-quoted name of the variables, which `ggplot::aes()` doesn't understand. As far as I'm concerned, the functions `cat()` and `as.name()` can take the string-quoted variable name and return it in the very typical way that `aes()` understands; unquoted. However, neither of them worked. The following reprex reproduces the problem. I commented the code to spare you the pain of figuring out what the code does.


```{r}

library(tidyverse)

# Divide the classes of variables into numeric and non-numeric. Goal: place a combination of numeric variables on the axes wwhile encoding a non-numeric variable.
mpg_numeric <- map_lgl(.x = seq_along(mpg), .f = ~ mpg[[.x]] %>% class() %in% c("numeric","integer"))
mpg_factor <- map_lgl(.x = seq_along(mpg), .f = ~ mpg[[.x]] %>% class() %in% c("factor","character"))

# create all possible combinations of the variables
eda_routine_combinations <- expand_grid(num_1 = mpg[mpg_numeric] %>% names(), 
                                        num_2 = mpg[mpg_numeric] %>% names(), 
                                        fct   = mpg[mpg_factor] %>% names()) %>% 
  filter(num_1 != num_2) %>%  slice_head(n = 2) # for simplicity, keep only the first 2 combinations

# use purrr::pmap() to create all the plots we want in a single call
pmap(.l = list(eda_routine_combinations$num_1, 
               eda_routine_combinations$num_2,
               eda_routine_combinations$fct) ,  
     .f = ~ mpg %>%
  ggplot(aes(..1 , ..2, col = ..3)) + 
  geom_point() )


```

Next we pinpoint the problem using a typical *ggplot2* call.

this is what we want `purrr::pmap()` to create in its iterations:

```{r}
mpg %>%  
  ggplot(aes(displ , cyl, fill = drv)) + 
  geom_boxplot()
```

However, this is `purrr::pmap()` renders; quoted variable names:

```{r}
mpg %>% 
  ggplot(aes("displ" , "cyl", fill = "drv")) + 
  geom_boxplot()
 
```


***

# Failing attempts

Using `cat()` to transform the quoted variable names from `pmap()` into unquoted form for `aes()` to understand fails.

```{r}
mpg %>%  
  ggplot(aes(cat("displ") , cat("cyl"), fill = cat("drv"))) + 
  geom_boxplot()
```

Using `as.name()` to transform the quoted variable names from `pmap()` into unquoted form for `aes()` to understand fails.

```{r}
mpg %>%  
  ggplot(aes(as.name("displ") , as.name("cyl"), fill = as.name("drv"))) + 
  geom_boxplot()
 
```

***

# Bottom line

Is there a way to make `ggplot(aes("quoted_var_name"))` work properly? 

joelschwartz.com

unread,
Dec 2, 2021, 12:24:17 AM12/2/21
to Hossam Ghorab, ggplot2
Hi Hossam,

When you have column names as character strings, you can use them inside aes by first turning them into symbols with the sym() function and then evaluating those symbols with !! (bang-bang). The code below adapts your example to show this. I’ve also tried to shorten the code for creating the data frame with the names of the columns we want to include in each plot. Please let me know if you have any questions.

Best Wishes,
Joel

library(tidyverse)
library(patchwork) # For laying out plots

# Create a data frame with all combinations of two numeric columns 
#  and one category column
vars = crossing(
  as_tibble(t(combn(select(mpg, where(is.numeric)) %>% names, 2))),
  V3=select(mpg, where(~!is.numeric(.))) %>% names
)

# Plot the first three combinations
plot.list = vars[1:3, ] %>% 
  pmap(
    ~ggplot(mpg, aes(!!sym(..1), !!sym(..2), colour=!!sym(..3))) +
      geom_point()
  )

wrap_plots(plot.list)



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/7bab2466-75d6-4108-a9a2-693433490a90n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages