2015-10-16 53 views
1

我下面从ggplot documentationGGPLOT2与ablines仅

的例子这是代码:

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)) 

不过,我想相同的图形与只有“ablines”!没有点! 尝试:

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

给我一个错误:“参数‘ENV’缺失,没有默认值”

回答

6

您可以使用geom_blank()这将计算限制(等人),但然后让你只需要使用geom_abline

p + geom_blank() + geom_abline(data=coefs, aes(intercept=a, slope=b)) 

enter image description here