gpt4 book ai didi

r - ggplot2 带有空格和 tidyeval 语法的变量名

转载 作者:行者123 更新时间:2023-12-05 00:52:39 27 4
gpt4 key购买 nike

我正在尝试创建一个允许用户传入列名的函数,我发现生成的图只是屏幕中间的一个点。需要进行哪些更改才能使其按预期工作?我的极简示例是:

library(ggplot2)
weather <- data.frame(Month = month.name[1:5], `Low Temperature` = 20:16,
`High Temperature` = seq(30, 22, -2), check.names = FALSE)

xVariable <- "Month"
yVariable <- "High Temperature"
yVariable <- enquo(yVariable)
ggplot(weather, aes(x = xVariable, y = !!yVariable)) + geom_point()

最佳答案

因为它是引用文本,而不是 enquo,所以使用 rlang::sym

xVariable <- "Month"
yVariable <- "High Temperature"
fun <- function(dat, xvar, yvar) {
xvar <- rlang::sym(xvar)
yvar <- rlang::sym(yvar)
p1 <-dat %>%
ggplot(aes(x = !!xvar, y = !!yvar)) +
geom_point()
return(p1)

}


fun(weather, xVariable, yVariable)

enter image description here

关于r - ggplot2 带有空格和 tidyeval 语法的变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69672585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com