Reputation: 754
I have done two methods for calculating a lm
for an abline
on my graph and neither of them work.
I have created a lm
for every column of the original data before using melt
.
model1 <- lm(Starling ~ Years, Farmland_Total)
structure(list(Years = 1994:2013, Starling = c(13260L, 15551L,
16335L, 18997L, 18571L, 18376L, 15770L, 6819L, 16054L, 15101L,
16276L, 19816L, 21928L, 26432L, 22375L, 21848L, 20689L, 20203L,
21061L, 21591L), Skylark = c(13520L, 15571L, 16275L, 19067L,
18840L, 18926L, 15810L, 6769L, 16114L, 15161L, 16476L, 20466L,
22701L, 27909L, 23150L, 22633L, 21862L, 21276L, 22341L, 23134L
), YellowWagtail = c(7194L, 8129L, 8593L, 9766L, 9578L, 9836L,
8146L, 3530L, 7964L, 8250L, 9195L, 10387L, 11143L, 12491L, 10097L,
10908L, 10087L, 10434L, 10529L, 10240L)), row.names = c(NA, -20L
), class = "data.frame")
Further Example of my data:
'data.frame': 20 obs. of 33 variables:
$ Years : int 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 ...
$ Starling : int 13260 15551 16335 18997 18571 18376 15770 6819 16054 15101 ...
$ Skylark : int 13520 15571 16275 19067 18840 18926 15810 6769 16114 15161 ...
$ YellowWagtail: int 7194 8129 8593 9766 9578 9836 8146 3530 7964 8250 ...
$ Kestrel : int 12620 14721 15575 18397 18161 17916 15200 6549 15194 14331 ...
$ Yellowhammer : int 13027 15236 15902 18686 18373 18518 15479 6709 15846 14845 ...
$ Greenfinch : int 12957 15201 16002 18794 18541 18626 15747 6759 16004 14988 ...
$ Swallow : int 13440 15561 16285 19077 18841 18746 15810 6759 16104 15241 ...
$ Lapwing : int 10212 12106 12615 14819 14772 14618 12122 4979 12076 11718 ...
$ Housemartin : int 10337 12461 12931 14884 14955 14736 12657 5929 13197 12218 ...
$ Linnet : int 12812 15051 15705 18317 17991 18456 15440 6549 15647 14911 ...
$ GreyPartridge: int 9922 11406 11774 13248 12846 13248 11322 3938 11276 10588 ...
$ TurtleDove : int 10240 12151 12684 14664 14631 14503 12583 5499 13084 12541 ...
$ Cornbunting : int 5362 5749 6298 7273 6815 7121 6041 2698 5898 5811 ...
Now example of data after melt
(used library(reshape2
):
structure(list(Years = c(1994L, 2003L, 2013L, 2003L, 2013L, 2003L,
2013L), Species = structure(1:7, .Label = c("Starling", "YellowWagtail",
"Yellowhammer", "Housemartin", "GreyPartridge", "Bullfinch",
"Blackbird"), class = "factor"), Farmland = c(13260L, 8250L,
21674L, 12218L, 14358L, 10588L, 23271L)), row.names = c(1L, 50L,
100L, 150L, 200L, 250L, 300L), class = "data.frame")
A further example:
'data.frame': 600 obs. of 3 variables:
$ Years : int 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 ...
$ Species : Factor w/ 30 levels "Starling","Skylark",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Farmland: int 13260 15551 16335 18997 18571 18376 15770 6819 16054 15101 ...
My lm
after melt
:
model2 <- lm(Farmland ~ Species, Work_practice)
My graph: Graph of Data (melt)
ERROR messages from model1
:
abline(model1)
Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
2: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
4: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
ERROR messages from model2
:
abline(model2)
Warning message:
In abline(model2) : only using the first two of 30 regression coefficients
model2
Does not actually use any of the regression lines because still none are plotted.
Is there a better way to have abline show the lm
across all objects?
An example of my plot code:
library(ggplot2)
ggplot(work_practice, aes(Years, Farmland, colour = Species)) + geom_line()
model1 <- lm(Farmland~Species, work_practice)
I expect abline
to create multiple lines like this graph:
Upvotes: 0
Views: 1573
Reputation: 30474
If using ggplot2
you probably want geom_abline
and not the base graphics abline
.
For a basic model with one predictor, you can use the slope and intercept from your linear model for geom_abline
, if you wished to approach this using model information in this way:
library(ggplot2)
library(data.table)
model1 <- lm(Starling ~ Years, Farmland_Total)
ggplot(Farmland_Total, aes(Years, Starling)) +
geom_line() +
geom_abline(slope = model1$coefficients[["Years"]], intercept = model1$coefficients[["(Intercept)"]])
For your multivariate model, and as an alternative approach, you can also use geom_smooth
with method=lm
to get (single or) multiple regression lines:
Work_practice <- melt(Farmland_Total,
id.vars = "Years",
measure.vars = c("Starling", "Skylark", "YellowWagtail"),
variable.name = "Species",
value.name = "Farmland")
ggplot(Work_practice, aes(Years, Farmland, colour = Species)) +
geom_line() +
geom_smooth(method = "lm", se = FALSE)
Upvotes: 0