【问题标题】:tidy eval ggplot2 NSE not rendering correctlytidy eval ggplot2 NSE 无法正确渲染
【发布时间】:2020-10-02 14:55:50
【问题描述】:

我正在尝试编写一个函数来传递引用的项目以构建多个 ggplots。以下代码效果很好,可以满足我的需求。

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))

使用this guide我构造了以下函数并调用了该函数。

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")

我的图形呈现没有错误,但呈现方式不同。 我做错了什么?

【问题讨论】:

  • 请分享示例数据以使您的问题可重现
  • 在调用函数时尝试不带引号,即plotf(x = Surgery, y = BALF_Protein, fill = Exposure, facet = Duration)
  • 使函数使用带引号的变量名的另一个选项是使用!!sym(varname).datapronoun,即.data[[varname]]。在这两种情况下,您都不需要 enquo() 变量。

标签: r ggplot2 eval tidy nse


【解决方案1】:

谢谢@Stefan

我不明白为什么,但是按照您的建议调用它是可行的。当我想遍历一个变量名向量来调用函数并且这些变量名将作为引用传递时,这将如何工作。使用syms() ?

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

这里有一些 rnorm() 的 ReproData,因此您的绘图高度可能略有不同。

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")

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-07-21
  • 2015-03-10
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多