geom_hline 似乎无视美学,我是否遗漏了什么或者这是一个错误?
df <- data.frame(
"x"=rep(c(1,2,3),2),
"y"=rep(c(1,2,3),2),
"g"=rep(c(TRUE,FALSE),each=3)
)
ggplot(df,aes(x=x,y=y)) +
geom_point() +
geom_hline(yintercept = 1.5,aes(colour=g)) +
facet_wrap(~g)
这将作为没有颜色指南和所有黑线的绘图返回
我得到同样的东西,例如
linetype=g
在美学作品之外设置颜色,例如
geom_hline(yintercept = 1.5,colour="red") +
session 信息:
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plotly_4.8.0 bindrcpp_0.2.2 rvest_0.3.2 xml2_1.2.0 forcats_0.3.0 stringr_1.3.1
[7] dplyr_0.7.6 purrr_0.2.5 readr_1.1.1 tidyr_0.8.1 tibble_1.4.2 ggplot2_3.0.0
[13] tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.19 lubridate_1.7.4 lattice_0.20-35 assertthat_0.2.0 rprojroot_1.3-2
[6] digest_0.6.17 utf8_1.1.4 mime_0.6 R6_2.3.0 cellranger_1.1.0
[11] plyr_1.8.4 backports_1.1.2 evaluate_0.11 httr_1.3.1 pillar_1.3.0
[16] rlang_0.2.2 lazyeval_0.2.1 curl_3.2 readxl_1.1.0 rstudioapi_0.8
[21] data.table_1.11.8 rmarkdown_1.10 labeling_0.3 selectr_0.4-1 htmlwidgets_1.3
[26] munsell_0.5.0 shiny_1.1.0 broom_0.5.0 compiler_3.4.4 httpuv_1.4.5
[31] modelr_0.1.2 pkgconfig_2.0.2 base64enc_0.1-3 htmltools_0.3.6 tidyselect_0.2.4
[36] fansi_0.4.0 viridisLite_0.3.0 crayon_1.3.4 withr_2.1.2 later_0.7.5
[41] grid_3.4.4 nlme_3.1-137 jsonlite_1.5 xtable_1.8-3 gtable_0.2.0
[46] magrittr_1.5 scales_1.0.0 cli_1.0.1 stringi_1.2.4 promises_1.0.1
[51] RColorBrewer_1.1-2 tools_3.4.4 Cairo_1.5-9 glue_1.3.0 hms_0.4.2
[56] crosstalk_1.0.0 yaml_2.2.0 colorspace_1.3-2 knitr_1.20 bindr_0.1.1
[61] haven_1.1.2
最佳答案
yintercept
也必须在 aes
内,colour
映射才能工作。
ggplot(df,aes(x=x,y=y)) +
geom_point() +
geom_hline(aes(yintercept = 1.5, colour=g)) +
facet_wrap(~g)
我不知道为什么
geom_hline
以这种方式工作。为了与其他 ggplot geoms 保持一致,似乎 yintercept
应该独立于其他美学。例如, colour
美学在这种情况下按预期工作:ggplot(df,aes(x=x,y=y)) +
geom_point() +
geom_line(y=1, aes(colour=g)) +
facet_wrap(~g)
更新: 感谢@Anonymouscoward 为我指出
geom_abline/vline/hline
的帮助,这解释了这些 geoms 无法识别 x
和 y
美学:有关其他详细信息,请参阅帮助。
关于r - 如何使用美学为 geom_hline() 着色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52860492/