r - Abline不会显示

我已经完成了两种方法来计算图上的ablinelm ,但它们都不起作用。

在使用melt之前,我已经为原始数据的每一列创建了一个lm

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

我的数据的其他示例:

'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 ...

现在是melt后的数据示例(使用了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")

另一个例子:

'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 ...

我的lm melt后:

model2 <- lm(Farmland ~ Species, Work_practice)

我的图: 数据图(融解)

来自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

来自model2 错误消息:

abline(model2)
Warning message:
In abline(model2) : only using the first two of 30 regression coefficients

model2实际上不使用任何回归线,因为仍然没有绘制任何回归线。

有没有一种更好的方法可以让abline在所有对象上显示lm

我的情节代码的示例:

library(ggplot2)
ggplot(work_practice, aes(Years, Farmland, colour = Species)) + geom_line()
model1 <- lm(Farmland~Species, work_practice)

我希望abline可以创建多条线,如下图所示:

绝对曲线图(绘制在油漆上)

如果使用ggplot2 ,则可能需要geom_abline而不是基本图形abline

对于具有一个预测变量的基本模型,如果希望以这种方式使用模型信息来实现此geom_abline ,则可以对geom_abline使用线性模型的斜率和截距:

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

简单线性模型的示例示例

对于您的多元模型,作为一种替代方法,您也可以将geom_smoothmethod=lm一起使用以获取(单或多条)回归线:

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)

具有多元回归和geom_smooth的示例退位

转载请注明来自askonline.tech,本文标题:r - Abline不会显示


 Top