ggplot 认为美学不是通过 `aes()` 产生的,但它是

ggplot thinks aesthetic wasn't made via `aes()`, but it was

我经常使用 ggplot2,我不得不说这个让我感到困惑。

脚本是:

library(tidyverse)

data_frame(value = rbinom(n = 100, size = 100, prob = 0.3)) %>%
  ggplot(mapping = aes(x = value)) %>%
  geom_histogram()

sessionInfo()

输出为:

> library(tidyverse)
Loading tidyverse: ggplot2
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
Loading tidyverse: dplyr
Conflicts with tidy packages -------------------------------------------------------
filter(): dplyr, stats
lag():    dplyr, stats
> 
> data_frame(value = rbinom(n = 100, size = 100, prob = 0.3)) %>%
+   ggplot(mapping = aes(x = value)) %>%
+   geom_histogram()
Error: Mapping must be created by `aes()` or `aes_()`
> 
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.5.0     purrr_0.2.2     readr_1.0.0     tidyr_0.6.0     tibble_1.2     
[6] ggplot2_2.2.1   tidyverse_1.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8      assertthat_0.1   R6_2.2.0         grid_3.3.2      
 [5] plyr_1.8.4       DBI_0.5-1        gtable_0.2.0     magrittr_1.5    
 [9] scales_0.4.1     lazyeval_0.2.0   tools_3.3.2      munsell_0.4.3   
[13] colorspace_1.3-2

用加号试试,我每周至少犯一次这个错误。

data_frame(value = rbinom(n = 100, size = 100, prob = 0.3)) %>%
  ggplot(mapping = aes(x = value)) +
  geom_histogram()