在 ggplot2 中的一个图上绘制多条回归线
Plot multiple regression lines on one plot in ggplot2
抱歉,如果这是一个重复的问题,但我还没有设法找到答案,因为我的数据框必须被拆分。我试图在一个图上绘制两条回归线,其中一条回归线用于第 1 期 (1815-1899) 的数据,一条回归线用于第 2 期 (1900-2013) 的数据。我已经使用 dplyr
将数据拆分为 运行 两个独立的回归,但无法弄清楚如何将它们放在同一个图表上,因为您似乎需要 [=12= 中的数据框] 命令绘制线。有人可以帮忙吗?
谢谢。
library(tidyverse)
brest<-read.csv("brest.csv",header=TRUE) ## read in csv
brest<- na.omit(brest) ## get rid of NAs
brestp1<- select(filter(brest, period == 1),c(year,slr,period)) ## Divide into periods
brestp2<- select(filter(brest, period == 2),c(year,slr,period))
fit1 <- lm(slr ~ year, data = brestp1) ## Run lms
summary(fit1)
fit2<- lm(slr ~ year, data = brestp2)
summary(fit2)
## plot graph
ggplot(brestp1, aes(x = year, y = slr)) + ### Need not only brestp1 but also brestp2
geom_point() +
stat_smooth(method = "lm",se=FALSE)+
theme_classic()
## Data
## Brest period 1
structure(list(year = 1815:1820, slr = c(6926L, 6959L, 6945L,
6965L, 6941L, 6909L), period = c(1L, 1L, 1L, 1L, 1L, 1L)), na.action = structure(c(`30` = 30L,
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L,
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L,
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")
##Brest period 2
structure(list(year = 1900:1905, slr = c(6936L, 6916L, 6923L,
6976L, 6931L, 6913L), period = c(2L, 2L, 2L, 2L, 2L, 2L)), na.action = structure(c(`30` = 30L,
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L,
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L,
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")
将 geom_smooth
与单独的数据一起使用:
ggplot() +
geom_smooth(aes(x = year, y = slr), data = brest1,
method = "lm", se = FALSE, color = "red") +
geom_smooth(aes(x = year, y = slr), data = brest2,
method = "lm", se = FALSE, color = "blue") +
geom_point(aes(x = year, y = slr), data = brest1, color = "red") +
geom_point(aes(x = year, y = slr), data = brest2, color = "blue")
抱歉,如果这是一个重复的问题,但我还没有设法找到答案,因为我的数据框必须被拆分。我试图在一个图上绘制两条回归线,其中一条回归线用于第 1 期 (1815-1899) 的数据,一条回归线用于第 2 期 (1900-2013) 的数据。我已经使用 dplyr
将数据拆分为 运行 两个独立的回归,但无法弄清楚如何将它们放在同一个图表上,因为您似乎需要 [=12= 中的数据框] 命令绘制线。有人可以帮忙吗?
谢谢。
library(tidyverse)
brest<-read.csv("brest.csv",header=TRUE) ## read in csv
brest<- na.omit(brest) ## get rid of NAs
brestp1<- select(filter(brest, period == 1),c(year,slr,period)) ## Divide into periods
brestp2<- select(filter(brest, period == 2),c(year,slr,period))
fit1 <- lm(slr ~ year, data = brestp1) ## Run lms
summary(fit1)
fit2<- lm(slr ~ year, data = brestp2)
summary(fit2)
## plot graph
ggplot(brestp1, aes(x = year, y = slr)) + ### Need not only brestp1 but also brestp2
geom_point() +
stat_smooth(method = "lm",se=FALSE)+
theme_classic()
## Data
## Brest period 1
structure(list(year = 1815:1820, slr = c(6926L, 6959L, 6945L,
6965L, 6941L, 6909L), period = c(1L, 1L, 1L, 1L, 1L, 1L)), na.action = structure(c(`30` = 30L,
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L,
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L,
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")
##Brest period 2
structure(list(year = 1900:1905, slr = c(6936L, 6916L, 6923L,
6976L, 6931L, 6913L), period = c(2L, 2L, 2L, 2L, 2L, 2L)), na.action = structure(c(`30` = 30L,
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L,
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L,
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L,
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")
将 geom_smooth
与单独的数据一起使用:
ggplot() +
geom_smooth(aes(x = year, y = slr), data = brest1,
method = "lm", se = FALSE, color = "red") +
geom_smooth(aes(x = year, y = slr), data = brest2,
method = "lm", se = FALSE, color = "blue") +
geom_point(aes(x = year, y = slr), data = brest1, color = "red") +
geom_point(aes(x = year, y = slr), data = brest2, color = "blue")