ggplot2只有ablines

van*_*eri 1 plot r ggplot2

我正在关注ggplot文档中的一个示例

这是代码:

library(plyr)
coefs <- ddply(mtcars, .(cyl), function(df) {
  m <- lm(mpg ~ wt, data=df)
  data.frame(a = coef(m)[1], b = coef(m)[2])
})

p <- ggplot(mtcars, aes(x = wt, y=mpg), . ~ cyl) + geom_point()
p + geom_abline(data=coefs, aes(intercept=a, slope=b))
Run Code Online (Sandbox Code Playgroud)

但是,我想只使用'ablines'相同的图形!没有点!试:

p <- ggplot(mtcars, aes(x = wt, y=mpg), . ~ cyl)
p + geom_abline(data=coefs, aes(intercept=a, slope=b))
Run Code Online (Sandbox Code Playgroud)

给我一个错误:"参数"env"缺失,没有默认值'

hrb*_*str 6

您可以使用geom_blank()哪个来计算限制(等)但是让您只使用geom_abline:

p + geom_blank() + geom_abline(data=coefs, aes(intercept=a, slope=b))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述