Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R tidy eval ggplot2 NSE未正确渲染_R_Ggplot2_Eval_Tidy_Nse - Fatal编程技术网

R tidy eval ggplot2 NSE未正确渲染

R tidy eval ggplot2 NSE未正确渲染,r,ggplot2,eval,tidy,nse,R,Ggplot2,Eval,Tidy,Nse,我正在尝试编写一个函数来传递引用项以构建多个GGPlot 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

我正在尝试编写一个函数来传递引用项以构建多个GGPlot

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

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

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谢谢@Stefan

我不明白为什么,但按照你的建议打电话是有效的。当我想循环一个变量名向量来调用函数,而这些变量名将以引号的形式传递时,这将如何工作。使用
syms()

在此处使用一些
rnorm()
重新编辑数据,这样您的绘图高度可能会略有不同

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

fig2.data调用函数时,请共享示例数据,以使您的问题不带引号地再现,即plotf(x=Surgery,y=BALF_Protein,fill=Exposure,facet=Duration)另一个使函数使用带引号的变量名工作的选项是使用
!!sym(varname)
.data
代词,即
.data[[varname]]
。在这两种情况下,你都不需要对你的VAR进行补偿。这可能会有所帮助
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")