ggplot认为美学不是通过`aes()`制作的,但确实如此

时间:2017-01-13 16:06:22

标签: r ggplot2 aesthetics

我经常使用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

1 个答案:

答案 0 :(得分:10)

尝试加号,我每周至少犯一次错误。

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