2

我想在数据ggplot2区域之外的数据子集上推断数据的线性拟合(以便可视化推断的危险)。在下面的代码中,我想将在 1980-1990 帧上生成的线性拟合扩展到 1990-2000,这样我就可以为整个时间段添加完全拟合并可视化差异:

set.seed(123)           
frame <- data.frame(year = rep(1980:2000, 10), y = sample(1:1000, 210))
head(frame)

frame1 <- frame[frame$year %in% c(1980:1990),]
frame2 <- frame[frame$year %in% c(1980:2000),]

ggplot(frame1, aes(x = year, y = y)) + geom_point(shape = 1) + geom_smooth(method = lm) + xlim(1980, 2000)
ggplot(frame2, aes(x = year, y = y)) + geom_point(shape = 1) + geom_smooth(method = lm) + xlim(1980, 2000)

我是新手,ggplot2所以关于如何从第一帧扩展线性拟合,然后添加数据和不同颜色的新拟合会很棒。谢谢。

4

1 回答 1

6

你可以试试

ggplot(frame2, aes(x = year, y = y)) + 
  geom_point(shape = 1) + 
  geom_smooth(method = lm, se=FALSE) + 
  geom_smooth(data=frame1, method = lm, fullrange=TRUE, se=FALSE, colour="red") + 
  xlim(1980, 2000) 

在此处输入图像描述

于 2015-11-11T17:54:02.617 回答