如何使用 GGplot2 在 R 中的同一个图上绘制两个独立的线性回归?
How to plot two independent linear regressions on the same plot in R using GGplot2?
我试图在同一个图上呈现两个数据集的线性回归。
- 鸟类数据与年份
- 符号数据与年份
由于它们彼此排斥(计算来自两个岛屿的数据),我不想绘制多元回归,但我不确定 R 中的代码在同一图上产生两种回归。
PlasticMass.data
ï..Year |Bird.Plastic.Mass Signy.Plastic.Mass
1 1991 | NA | 2.384
2 1992 | NA | 8.340
3 1993 | NA | 2.680
4 1994 | NA | 1.450
5 1995 | NA | 1.940
6 1996 | 6.43 | 0.570
7 1997 | 19.86| 1.170
8 1998 | 4.89 | 2.010
9 1999 | 2.97 | 1.410
10 2000 | 3.10 | 1.690
11 2001 | 3.30 | 0.350
12 2002 | 4.45 | 9.280
13 2003 | 4.05 | 16.750
14 2004 | 2.18 | 4.330
15 2005 | 4.88 | 0.260
16 2006 | 4.39 | 13.500
17 2007 | 4.27 | 6.270
18 2008 | 4.40 | 9.030
19 2009 | 1.63 | 3.860
20 2010 | 1.70 | 22.100
21 2011 | 1.64 | 1.150
22 2012 | 2.16 | 13.080
23 2013 | 3.05 | 0.140
24 2014 | 1.34 | 0.010
25 2015 | 3.66 | 0.000
26 2016 | 0.87 | 0.000
27 2017 | 1.10 | 7.010
28 2018 | 2.29 | 1.740
29 2019 | 1.44 | 80.790
绘制单个回归的 R 代码:
塑料质量线性回归
PlasticMass.data <-read.csv("Plastic by Mass.csv", header = T)
print(Plastic.Mass.data)
modelPB <-lm(Bird.Plastic.Mass ~ï..Year, data= PlasticMass.data)
modelPS <-lm(Signy.Plastic.Mass ~ï..Year, data = PlasticMass.data)
ggplot(PlasticMass.data, aes(ï..Year, Bird.Plastic.Mass))+
geom_point()+
geom_smooth(method = "lm")+
labs(x="Year", y="Bird Island Total Debris Count")
ggplot(PlasticMass.data, aes(ï..Year, Signy.Plastic.Mass))+
geom_point()+
geom_smooth(method = "lm", colour ="lightgreen")+
labs(x="Year", y="Signy Island Total Debris Count")
这里是 link 显示我在 excel 上绘制的回归图(其中绘制了两个数据集并显示了单独的线性回归)。
Regressions in excel
方法
转向更长,使用组映射将转向组映射到 lm
代码
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
mutate(Bird.Plastic.Mass = as.numeric(trimws(Bird.Plastic.Mass)),
Year = factor(Year))%>%
na.omit() %>%
pivot_longer(cols = Bird.Plastic.Mass:Signy.Plastic.Mass, names_to = "var", values_to="val") %>%
ggplot(aes(Year, val, col=var, group=var))+
geom_point() +
geom_smooth(method="lm")
结果(与Excel图不完全一样,可能是数据少)
数据
df <- structure(list(Year = c("1 1991 ", "2 1992 ", "3 1993 ",
"4 1994 ", "5 1995 ", "6 1996 ", "7 1997 ", "8 1998 ",
"9 1999 ", "10 2000 ", "11 2001 ", "12 2002 ", "13 2003 ",
"14 2004 ", "15 2005 ", "16 2006 ", "17 2007 ", "18 2008 ",
"19 2009 ", "20 2010 ", "21 2011 ", "22 2012 ", "23 2013 ",
"24 2014 ", "25 2015 ", "26 2016 ", "27 2017 ", "28 2018 ",
"29 2019 "), Bird.Plastic.Mass = c(" NA ", " NA ",
" NA ", " NA ", " NA ",
" 6.43 ", " 19.86", " 4.89 ",
" 2.97 ", " 3.10 ", " 3.30 ",
" 4.45 ", " 4.05 ", " 2.18 ",
" 4.88 ", " 4.39 ", " 4.27 ",
" 4.40 ", " 1.63 ", " 1.70 ",
" 1.64 ", " 2.16 ", " 3.05 ",
" 1.34 ", " 3.66 ", " 0.87 ",
" 1.10 ", " 2.29 ", " 1.44 "
), Signy.Plastic.Mass = c(2.384, 8.34, 2.68, 1.45, 1.94, 0.57,
1.17, 2.01, 1.41, 1.69, 0.35, 9.28, 16.75, 4.33, 0.26, 13.5,
6.27, 9.03, 3.86, 22.1, 1.15, 13.08, 0.14, 0.01, 0, 0, 7.01,
1.74, 80.79)), class = "data.frame", row.names = c(NA, -29L))
我试图在同一个图上呈现两个数据集的线性回归。
- 鸟类数据与年份
- 符号数据与年份 由于它们彼此排斥(计算来自两个岛屿的数据),我不想绘制多元回归,但我不确定 R 中的代码在同一图上产生两种回归。
PlasticMass.data
ï..Year |Bird.Plastic.Mass Signy.Plastic.Mass
1 1991 | NA | 2.384
2 1992 | NA | 8.340
3 1993 | NA | 2.680
4 1994 | NA | 1.450
5 1995 | NA | 1.940
6 1996 | 6.43 | 0.570
7 1997 | 19.86| 1.170
8 1998 | 4.89 | 2.010
9 1999 | 2.97 | 1.410
10 2000 | 3.10 | 1.690
11 2001 | 3.30 | 0.350
12 2002 | 4.45 | 9.280
13 2003 | 4.05 | 16.750
14 2004 | 2.18 | 4.330
15 2005 | 4.88 | 0.260
16 2006 | 4.39 | 13.500
17 2007 | 4.27 | 6.270
18 2008 | 4.40 | 9.030
19 2009 | 1.63 | 3.860
20 2010 | 1.70 | 22.100
21 2011 | 1.64 | 1.150
22 2012 | 2.16 | 13.080
23 2013 | 3.05 | 0.140
24 2014 | 1.34 | 0.010
25 2015 | 3.66 | 0.000
26 2016 | 0.87 | 0.000
27 2017 | 1.10 | 7.010
28 2018 | 2.29 | 1.740
29 2019 | 1.44 | 80.790
绘制单个回归的 R 代码: 塑料质量线性回归
PlasticMass.data <-read.csv("Plastic by Mass.csv", header = T)
print(Plastic.Mass.data)
modelPB <-lm(Bird.Plastic.Mass ~ï..Year, data= PlasticMass.data)
modelPS <-lm(Signy.Plastic.Mass ~ï..Year, data = PlasticMass.data)
ggplot(PlasticMass.data, aes(ï..Year, Bird.Plastic.Mass))+
geom_point()+
geom_smooth(method = "lm")+
labs(x="Year", y="Bird Island Total Debris Count")
ggplot(PlasticMass.data, aes(ï..Year, Signy.Plastic.Mass))+
geom_point()+
geom_smooth(method = "lm", colour ="lightgreen")+
labs(x="Year", y="Signy Island Total Debris Count")
这里是 link 显示我在 excel 上绘制的回归图(其中绘制了两个数据集并显示了单独的线性回归)。
Regressions in excel
方法
转向更长,使用组映射将转向组映射到 lm
代码
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
mutate(Bird.Plastic.Mass = as.numeric(trimws(Bird.Plastic.Mass)),
Year = factor(Year))%>%
na.omit() %>%
pivot_longer(cols = Bird.Plastic.Mass:Signy.Plastic.Mass, names_to = "var", values_to="val") %>%
ggplot(aes(Year, val, col=var, group=var))+
geom_point() +
geom_smooth(method="lm")
结果(与Excel图不完全一样,可能是数据少)
数据
df <- structure(list(Year = c("1 1991 ", "2 1992 ", "3 1993 ",
"4 1994 ", "5 1995 ", "6 1996 ", "7 1997 ", "8 1998 ",
"9 1999 ", "10 2000 ", "11 2001 ", "12 2002 ", "13 2003 ",
"14 2004 ", "15 2005 ", "16 2006 ", "17 2007 ", "18 2008 ",
"19 2009 ", "20 2010 ", "21 2011 ", "22 2012 ", "23 2013 ",
"24 2014 ", "25 2015 ", "26 2016 ", "27 2017 ", "28 2018 ",
"29 2019 "), Bird.Plastic.Mass = c(" NA ", " NA ",
" NA ", " NA ", " NA ",
" 6.43 ", " 19.86", " 4.89 ",
" 2.97 ", " 3.10 ", " 3.30 ",
" 4.45 ", " 4.05 ", " 2.18 ",
" 4.88 ", " 4.39 ", " 4.27 ",
" 4.40 ", " 1.63 ", " 1.70 ",
" 1.64 ", " 2.16 ", " 3.05 ",
" 1.34 ", " 3.66 ", " 0.87 ",
" 1.10 ", " 2.29 ", " 1.44 "
), Signy.Plastic.Mass = c(2.384, 8.34, 2.68, 1.45, 1.94, 0.57,
1.17, 2.01, 1.41, 1.69, 0.35, 9.28, 16.75, 4.33, 0.26, 13.5,
6.27, 9.03, 3.86, 22.1, 1.15, 13.08, 0.14, 0.01, 0, 0, 7.01,
1.74, 80.79)), class = "data.frame", row.names = c(NA, -29L))