簡體   English   中英

purrr和ggplot函數未顯示圖(NSE問題)

[英]purrr and ggplot function are not displaying plot (NSE issue)

我懷疑這是與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)

對我來說,這似乎不是NSE問題。 如果您閱讀?walk它說(我加了強調):

walk()呼叫.f產生副作用,並返回原始輸入

嘗試:

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

我認為,如果在ggplot添加顯式打印,則可以得到ggplot 例如,將temp1更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM