假设我有这样的数据:
n <- 100
set.seed(123)
df <- data.frame(
num1 = rnorm(n),
num2 = sample(1:4, n, replace = TRUE),
char = sample(c("Green", "Red", "Blue"), n, replace = TRUE),
fac = factor(sample(c("Green", "Red", "Blue"), n, replace = TRUE)),
logical = sample(c(TRUE, FALSE), n, replace = TRUE),
stringsAsFactors = FALSE
)
我想生成一个带有facets和参考线的ggplot ,以便在相应的facet中绘制参考线。例如:
## Base plot
ggplot(df, aes(num2, num1)) +
geom_jitter() +
facet_grid(~ char)
现在我想在logical == TRUE
时添加垂直线条。
类似的东西:
## Does not work
ggplot(df, aes(num2, num1)) +
geom_jitter() +
facet_grid(~ char) +
geom_vline(xintercept = num2)