我想在我的两个方面绘制两个不同的geom_abline
。这似乎与geom_hline
的工作方式不同 - 答案为here。
虽然
library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))
..有效,这个:
library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_abline(data = dummy2, aes(yintercept = Z), slope = 1)
没有:
Y-intercept不受影响。
答案 0 :(得分:4)
对于geom_abline
,您需要intercept
而不是yintercept
。此外,您需要在aes
内映射这两个值,否则设置slope
会覆盖您使用aes
设置的值。这有效:
library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) +
geom_point() +
facet_grid(~X) +
geom_abline(data = dummy2, aes(intercept = Z, slope = 1))