【问题标题】:purrr and ggplot function are not displaying plot (NSE issue)purrr 和 ggplot 函数未显示绘图(NSE 问题)
【发布时间】:2017-10-11 17:49:00
【问题描述】:

我怀疑这是处理 NSE 的问题。但是为什么这两种方法不起作用,我怎样才能让它们起作用。

temp1 <- function(x){
  iris %>% 
    ggplot(aes(Sepal.Length, Sepal.Width)) +
    geom_point() +
    facet_wrap(as.formula(paste("~", x)))
}
walk('Species', temp1)


temp2 <- function(x){
  x <- as.name(x)
  iris %>% 
    ggplot(aes(Sepal.Length, Sepal.Width)) +
    geom_point() +
    facet_wrap(~ x)
}
walk('Species', temp2)

【问题讨论】:

    标签: r ggplot2 purrr nse


    【解决方案1】:

    对我来说,这似乎不是 NSE 问题。如果您阅读?walk,它会说(重点由我添加):

    walk() 调用 .f 的副作用并返回原始输入

    试试:

    t <- walk('Species', temp1)
    t
    #[1] "Species"
    

    我认为如果您在ggplot 中添加显式打印,您可以获得所需的内容。例如。将temp1 更改为:

    temp1 <- function(x){
      print(iris %>% 
            ggplot(aes(Sepal.Length, Sepal.Width)) +
            geom_point() +
            facet_wrap(as.formula(paste("~", x))))
    }
    

    【讨论】:

    • 这是有道理的。您能否评论为什么通过管道(或者)这样做不起作用。那是... %&gt;% print()
    • @student,因为如果您使用简单的管道,您会将facet_wrap() 对象传递给print。如果你想管道,你可以用() 将整个东西分组,然后将它传递给print。类似(iris %&gt;% ...) %&gt;% print()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多