I'm having an issue while using pmap() with tidyeval and ggplot(). Not sure what I'm missing.
The code below works as desired:
library(tidyverse)
scatter_plot <- function(x,y,alpha, facet){
x_val <- enquo(x)
y_val <- enquo(y)
facet_var <- enquo(facet)
ggplot(iris, aes(!!x_val, !!y_val, alpha=alpha)) + geom_point() +
facet_wrap(vars(!!facet_var),ncol = 3)
}
scatter_plot(Petal.Length, Sepal.Length, 0.5, Species)
However using pmap() to pass a list of argument vectors doesn't give the desired plots:
x <- c("Petal.Length","Petal.Width")
y <- c("Sepal.Length","Sepal.Width")
alpha <- c(0.4,0.8)
facet <- rep("Species",2)
pmap(list(x,y,alpha,facet), scatter_plot)
#> [[1]]
#>
#> [[2]]
Created on 2018-11-06 by the reprex package (v0.2.1)
Hi, there is a big difference between how you use your function first time and second time. First time you are giving it unquoted symbols that you indeed need to use enquo
to capture. Second time you are giving it quoted input (strings), so enquo
no longer works since when unquoted with !!
it returns a string (e.g., "Petal.Width" or whatever, not symbol that you actually need).
So the solution is to use ensym
instead and then everything works. I've also changed pmap
to pwalk
since it clarifies that your output is a side-effect (i.e., printing plot to console):
library(tidyverse)
scatter_plot <- function(x,y,alpha, facet){
x_val <- ensym(x)
y_val <- ensym(y)
facet_var <- ensym(facet)
p <- ggplot(iris, aes(!!x_val, !!y_val, alpha=alpha)) + geom_point() +
facet_wrap(vars(!!facet_var),ncol = 3)
print(p)
}
#scatter_plot(Petal.Length, Sepal.Length, 0.5, Species)
x <- c("Petal.Length","Petal.Width")
y <- c("Sepal.Length","Sepal.Width")
alpha <- c(0.4,0.8)
facet <- rep("Species",2)
pwalk(list(x,y,alpha,facet), scatter_plot)
Created on 2018-11-06 by the reprex package (v0.2.1)
5 Likes
Thank you! I had tried getting rid of the quotes with noquote() before enquo() but that didn't work.
mara
November 6, 2018, 10:21am
4
If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).
Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
Thanks
system
Closed
November 13, 2018, 10:22am
5
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.