首页 > 解决方案 > Abline 不会显示

问题描述

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

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

我的lmmelt

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创建多条线,如下图:

Abline Graph(绘制在油漆上)

标签: rggplot2graphgraphics

解决方案


如果使用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)"]])

使用简单线性模型的示例

对于您的多变量模型,作为一种替代方法,您还可以使用geom_smoothwithmethod=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 的示例


推荐阅读