tidy eval ggplot2 NSE not rendering correctly

93 views Asked by At

I'm trying to write a function to pass quoted items for constructing multiple ggplots.The following code works great and does what I want.

fig2.data %>% 
  ggplot(aes(x = Surgery, y = BALF_Protein, fill = Exposure)) +
  stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge") +
  stat_summary(geom = "bar", fun = mean, position = "dodge") +
  theme_classic() +
  scale_fill_manual(values=c("lightgrey","darkgrey")) +
  facet_grid(cols = vars(Duration))

enter image description here

Using this guide I constructed the following function and called the function.

plotf <- function(x, y, fill, facet){
  
  x_var <- enquo(x)
  y_var <- enquo(y)
  facet_var <- enquo(facet)
  fill_var <- enquo(fill)
  
  ggplot(fig2.data, aes(x = !!x_var, y = !!y_var, fill = !!fill_var)) +
    stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge") +
    stat_summary(geom = "bar", fun = mean, position = "dodge") +
    theme_classic() +
    scale_fill_manual(values=c("lightgrey","darkgrey")) +
    facet_grid(cols = vars(!!facet_var))
}
plotf(x = "Surgery", y = "BALF_Protein", fill = "Exposure", facet = "Duration")

My graph rendered without errors, but it is not rendered the same way. What am I doing wrong?

enter image description here

1

There are 1 answers

1
akaDrHouse On

Thank you @Stefan

I don't understand why, but calling it as you suggested worked. How is that going to work when I want to loop over a vector of variable names to call the function and those are going to be passed as quoted. Use syms() ?

plotf(x = Surgery, y = BALF_Protein, fill = Exposure, facet = Duration)

ReproData here with some rnorm() so your plot might be slightly different heights.

fig2.data <- structure(list(Surgery = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("SHAM", "HEP VAG"
), class = "factor"), Exposure = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Air", 
"Ozone"), class = "factor"), Duration = structure(c(2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1d", 
"2d"), class = "factor"), BALF_Protein = c(64.2302655135303, 
75.8662498743628, 66.944160651771, 64.3494818599307, 93.5733806883362, 
93.9843061725941, 94.9296956493259, 85.5985055395191, 80.4974511604734, 
70.6316004306272, 85.3439438112908, 79.4666853120619, 84.7319693413318, 
224.606438793638, 78.4487502522719, 78.2128699744882, 92.0151032176434, 
79.2127901600167, 83.0909690767245, 92.0325415462662, 60.6200784843927, 
97.7183404856683, 68.7510921525122, 41.9625493809036, 311.769822036931, 
450.597937801349, 283.639976251784, 190.840750069959, 187.810222461528, 
203.735530975931, 547.003463243173, 517.871472878502, 164.167773487012, 
202.777306107217, 666.896662547508, 361.46103562071, 270.119121964956, 
234.635143377769, 94.4541075117046, 91.1060986818939, 142.774777316869, 
300.021992736686, 279.775933301683, 246.554185364089, 298.964364163939, 
193.737945537319, 232.918974192744, 150.384203703162)), row.names = c(NA, 
-48L), class = "data.frame")