Home > Blockchain >  How to plot two independent linear regressions on the same plot in R using GGplot2?
How to plot two independent linear regressions on the same plot in R using GGplot2?

Time:02-11

I am trying to present linear regressions of two datasets on the same plot.

  • Bird data vs year
  • Signy data vs year As they are exclusive to each other (count data from two islands) I don't want to plot a multiple regression, but I am not sure the code in R to produce both regressions on the same plot.

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 code to plot individual regressions: Plastic by mass linear regressions

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

Here is a link to show the regression plot I made on excel (where both datasets are plotted and the separate linear regressions are shown).

enter image description here

Data

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