Abline不会显示

时间:2019-11-28 23:07:52

标签: r ggplot2 graph graphics

我完成了两种方法来为图形上的lm计算abline,但它们都不起作用。

在使用lm之前,我已经为原始数据的每一列创建了一个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")

我的数据的进一步示例:

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

我的图: Graph of Data (melt)

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

ERROR 消息:

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

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

是否有更好的方法让所有对象上的lm都用斜线表示?

我的情节代码示例:

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

我希望abline创建如下图所示的多条线:

Abline Graph (Drawn on paint)

1 个答案:

答案 0 :(得分:0)

如果使用ggplot2,则可能需要geom_abline,而不是基本图形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)"]])

example abline with simple linear model

对于您的多元模型,作为一种替代方法,您还可以将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)

example abline with multivariate regression and geom_smooth