bang bang(!!)显然不在ggplot2里面工作

时间:2018-05-12 14:30:57

标签: r ggplot2 dplyr rlang

这是一个非常奇怪的问题,其他用户可以顺利运行此代码。但这对我不起作用。我不明白,也许是包的版本有问题。我正在运行R 3.4.4,tidyverse 1.2.1和rlang 0.2.0

代码如下:

library(tidyverse)
Q <- as_tibble(data.frame(series = rep(c("diax","diay"),3),
                          value = c(3.25,3.30,3.31,3.36,3.38,3.42),
                          year = c(2018,2018,2019,2019,2020,2020))) %>%
  select(year, series, value) %>% spread(key = "series", value = "value") %>%
  rename(`2018-01-01` = diax, `2017-01-01` = diay)

nomes <- c("2018-01-01","2017-01-01")
ggplot(Q, aes(x = year)) +
  geom_line(aes(y = !!as.name(nomes[1])), col = "red", size = 2, linetype = "dotdash") +
  geom_line(aes(y = !!as.name(nomes[2])), col = "orange", size = 2, linetype = "dashed")

或者,它也应该以这种方式工作:

library(rlang)
library(tidyverse)

Q <- as_tibble(data.frame(series = rep(c("diax","diay"),3),
                          value = c(3.25,3.30,3.31,3.36,3.38,3.42),
                          year = c(2018,2018,2019,2019,2020,2020))) %>%
  select(year, series, value) %>% spread(key = "series", value = "value") %>%
  rename(`2018-01-01` = diax, `2017-01-01` = diay)

nomes <- c("2018-01-01","2017-01-01")

    ggplot(Q, aes(x = year)) +
      geom_line(aes(y = !!as.name(nomes[1])), col = "red", size = 2, linetype = "dotdash") +
      geom_line(aes(y = !!as.name(nomes[2])), col = "orange", size = 2, linetype = "dashed")


    ggplot(Q, aes(x = year)) +
      geom_line(aes(y = !!rlang::sym(nomes[1])), col = "red", size = 2, linetype = "dotdash") +
      geom_line(aes(y = !!rlang::sym(nomes[2])), col = "orange", size = 2, linetype = "dashed")

问题是我一直收到以下错误消息:

Error in !as.name(nomes[1]) : invalid argument type

Error in !rlang::sym(nomes[1]) : invalid argument type

1 个答案:

答案 0 :(得分:1)

ggplot2 2.3.0将支持此功能,目前正准备发布。

library(dplyr)
library(tidyr)
library(ggplot2)
Q <- as_tibble(data.frame(series = rep(c("diax","diay"),3),
                          value = c(3.25,3.30,3.31,3.36,3.38,3.42),
                          year = c(2018,2018,2019,2019,2020,2020))) %>%
  select(year, series, value) %>% spread(key = "series", value = "value") %>%
  rename(`2018-01-01` = diax, `2017-01-01` = diay)

nomes <- c("2018-01-01","2017-01-01")
ggplot(Q, aes(x = year)) +
  geom_line(aes(y = !!as.name(nomes[1])), col = "red", size = 2, linetype = "dotdash") +
  geom_line(aes(y = !!as.name(nomes[2])), col = "orange", size = 2, linetype = "dashed")

reprex package(v0.2.0)创建于2018-05-12。

devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-05-12
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                          
#>  assertthat   0.2.0      2017-04-11 CRAN (R 3.5.0)                  
#>  backports    1.1.2      2017-12-13 CRAN (R 3.5.0)                  
#>  base       * 3.5.0      2018-04-24 local                           
#>  bindr        0.1.1      2018-03-13 CRAN (R 3.5.0)                  
#>  bindrcpp     0.2        2017-06-17 CRAN (R 3.5.0)                  
#>  colorspace   1.4-0      2017-12-23 R-Forge (R 3.5.0)               
#>  compiler     3.5.0      2018-04-24 local                           
#>  curl         3.1        2017-12-12 CRAN (R 3.5.0)                  
#>  datasets   * 3.5.0      2018-04-24 local                           
#>  devtools     1.13.5     2018-02-18 CRAN (R 3.5.0)                  
#>  digest       0.6.15     2018-01-28 CRAN (R 3.5.0)                  
#>  dplyr      * 0.7.4      2017-09-28 CRAN (R 3.5.0)                  
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.5.0)                  
#>  ggplot2    * 2.2.1.9000 2018-05-12 local                           
#>  glue         1.2.0      2017-10-29 CRAN (R 3.5.0)                  
#>  graphics   * 3.5.0      2018-04-24 local                           
#>  grDevices  * 3.5.0      2018-04-24 local                           
#>  grid         3.5.0      2018-04-24 local                           
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.5.0)                  
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.5.0)                  
#>  httr         1.3.1      2017-08-20 CRAN (R 3.5.0)                  
#>  knitr        1.20       2018-02-20 CRAN (R 3.5.0)                  
#>  labeling     0.3        2014-08-23 CRAN (R 3.5.0)                  
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.5.0)                  
#>  magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                  
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                  
#>  methods    * 3.5.0      2018-04-24 local                           
#>  mime         0.5        2016-07-07 CRAN (R 3.5.0)                  
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.5.0)                  
#>  pillar       1.2.1      2018-02-27 CRAN (R 3.5.0)                  
#>  pkgconfig    2.0.1      2017-03-21 CRAN (R 3.5.0)                  
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.5.0)                  
#>  purrr        0.2.4      2017-10-18 CRAN (R 3.5.0)                  
#>  R6           2.2.2      2017-06-17 CRAN (R 3.5.0)                  
#>  Rcpp         0.12.16    2018-03-13 CRAN (R 3.5.0)                  
#>  rlang        0.2.0.9001 2018-05-10 Github (r-lib/rlang@ccdbd8b)    
#>  rmarkdown    1.9        2018-03-01 CRAN (R 3.5.0)                  
#>  rprojroot    1.3-2      2018-01-03 CRAN (R 3.5.0)                  
#>  scales       0.5.0.9000 2018-04-10 Github (hadley/scales@d767915)  
#>  stats      * 3.5.0      2018-04-24 local                           
#>  stringi      1.1.7      2018-03-12 CRAN (R 3.5.0)                  
#>  stringr      1.3.0      2018-02-19 CRAN (R 3.5.0)                  
#>  tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                  
#>  tidyr      * 0.8.0      2018-01-29 CRAN (R 3.5.0)                  
#>  tidyselect   0.2.4      2018-02-26 CRAN (R 3.5.0)                  
#>  tools        3.5.0      2018-04-24 local                           
#>  utils      * 3.5.0      2018-04-24 local                           
#>  withr        2.1.2      2018-05-10 Github (jimhester/withr@79d7b0d)
#>  xml2         1.2.0      2018-01-24 CRAN (R 3.5.0)                  
#>  yaml         2.1.18     2018-03-08 CRAN (R 3.5.0)