考虑这段代码:
require(ggplot2)
ggplot(data = mtcars) +
geom_point(aes(x = drat, y = wt)) +
geom_hline(yintercept = 3) +
facet_grid(~ cyl) ## works
ggplot(data = mtcars) +
geom_point(aes(x = drat, y = wt)) +
geom_hline(yintercept = 3) +
facet_grid(~ factor(cyl)) ## does not work
# Error in factor(cyl) : object 'cyl' not found
# removing geom_hline: works again.
谷歌帮我找到了一个debug,即包裹intercept
成aes
ggplot(data = mtcars) +
geom_point(aes(x = drat, y = wt)) +
geom_hline(aes(yintercept = 3)) +
facet_grid(~ factor(cyl)) # works
# R version 3.4.3 (2017-11-30)
# ggplot2_2.2.1
Hadley 在这里写道,作为变量的函数需要存在于每一层中。(这对我来说听起来很神秘)
为什么在分解构面变量时会发生这种情况?