用ggplot约束stat_smooth中的斜率(绘制ANCOVA)

时间:2010-11-22 23:28:34

标签: r plot ggplot2

使用ggplot(),我试图绘制ANCOVA的结果,其中两个线性分量的斜率相等:即lm(y ~ x + A)geom_smooth(method = "lm")的默认行为是为每个因子的每个级别绘制单独的斜率和截距。例如,有两个级别A

library(ggplot2)
set.seed(1234)

n <- 20

x1 <- rnorm(n); x2 <- rnorm(n)
y1 <- 2 * x1 + rnorm(n)
y2 <- 3 * x2 + (2 + rnorm(n))
A <- as.factor(rep(c(1, 2), each = n))
df <- data.frame(x = c(x1, x2), y = c(y1, y2), A = A)

p <- ggplot(df, aes(x = x, y = y, color = A))
p + geom_point() + geom_smooth(method = "lm")

Default ggplot()

我可以将ANCOVA与lm()分开放置,然后使用geom_abline()手动添加行。这种方法有一些缺点,例如让线条超出数据范围并手动指定颜色。

fm <- lm(y ~ x + A, data = df)
summary(fm)

a1 <- coef(fm)[1]
b <- coef(fm)[2]
a2 <- a1 + coef(fm)[3]

p + geom_point() + 
  geom_abline(intercept = a1, slope = b) + 
  geom_abline(intercept = a2, slope = b)

With geom_abline()

我知道HH包中的ancova()会自动化绘图,但我并不关心格子图形。所以我正在寻找一个以ggplot()为中心的解决方案。

library(HH)
ancova(y ~ x + A, data = df)

是否有使用ggplot()完成此操作的方法?对于此示例,A有两个级别,但我有三个,四个或更多级别的情况。 formula的{​​{1}}参数似乎没有答案(据我所知)。

1 个答案:

答案 0 :(得分:5)

为了完整起见,这有效:

library(ggplot2)
set.seed(1234)

n <- 20

x1 <- rnorm(n); x2 <- rnorm(n)
y1 <- 2 * x1 + rnorm(n)
y2 <- 3 * x2 + (2 + rnorm(n))
A <- as.factor(rep(c(1, 2), each = n))
df <- data.frame(x = c(x1, x2), y = c(y1, y2), A = A)
fm <- lm(y ~ x + A, data = df)

p <- ggplot(data = cbind(df, pred = predict(fm)),
  aes(x = x, y = y, color = A))
p + geom_point() + geom_line(aes(y = pred))