我有一个(x,y)数据集,我想要想象以下内容:
我可以使用geom_point,geom_smooth和geom_abline通过ggplot完成这些任务。但是,我无法看到如何添加一个图例,让我可以注释三个图形对象的含义。此外,我的数据集只是x值的向量和y值的向量(不是数据帧)。
以下是代码示例:
library(ggplot2)
set.seed(100)
x = seq(-5, 1, length=50)
c0= 2
c1 = 1
y = c0 + c1*x + rnorm(length(x),sd = 0.5)
# Fit a linear model, y~x means y is the response
# variable to the x predictor variable
gp <- ggplot(mapping = aes(x=x,y=y))
gp <- gp + geom_point(col="black")
gp <- gp + geom_smooth(method=lm, formula = y~x, color="darkred", fill="red")
gp <- gp + geom_abline(intercept = 0.,slope = 0., lty="dashed", col="black")
plot(gp)
我希望包含一个标识数据的图例,最小二乘法拟合以及包含在内的水平线。