【问题标题】:Error in adding abline to scatterplot in R在 R 中将 abline 添加到散点图时出错
【发布时间】:2017-12-22 22:25:53
【问题描述】:

散点图

abundance = data.frame(lncount = c(13, 865, 1800), lnedna = c(4.72, 5.05,   5.22,   5.24,
                                                         5.36,  5.71,   5.63,   5.97,
                                                         6.08,  6.20,   6.43,   6.54))
xyplot(lnedna ~ lncount, data = abundance, 
   xlab = "Mussel Count by Snorkel Survey",
   ylab = "eDNA concentraion (gene sequence/Liter) on log scale", 
   main = "Mussel Abundance")

添加拟合线

abline(lm(lnedna ~ lncount))

这是我在尝试添加回归线时不断遇到的错误: eval(expr, envir, enclos) 中的错误:找不到对象 'lnedna'

lnedna 可以很好地制作散点图,为什么添加回归线不起作用?

【问题讨论】:

  • 将数据传递给lm函数:lm(lnedna ~ lncount, data=abundance)
  • 在旁注中,你必须使用xyplot()吗?
  • 为什么不plot()
  • @storaged 和 @InfiniteFlashChess 我也尝试了你的建议 #Add fit lines abline(lm(lnedna ~ lncount, data = pinch)) 但后来我收到此错误: int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new 尚未被调用。此外,我尝试了 plot() 并得到相同的错误

标签: r regression


【解决方案1】:

看来您需要在xyplot 调用中添加abline

lattice::xyplot(lnedna ~ lncount, data = abundance, 
                panel = function(x, y) {
                  lattice::panel.xyplot(x, y)
                  lattice::panel.abline(lm(y ~ x))
                },
                xlab = "Mussel Count by Snorkel Survey",
                ylab = "eDNA concentraion (gene sequence/Liter) on log scale", 
                main = "Mussel Abundance")

来自这个问题:How to add abline with lattice xyplot function?

【讨论】:

  • 这是问题的一部分,但不是全部。 xyplot() 似乎与 abline(lm(lnedna ~ ......)) 不匹配
  • 感谢@InfiniteFlashChess 已编辑答案以使用xyplot
  • 谢谢@hrabel。将 r 平方值添加到绘图的任何提示/代码?
  • @Ellen, latticeExtra 声称有这样的功能:latticeextra.r-forge.r-project.org/man/panel.lmlineq.html。没试过,但希望有效!
  • 您需要做的就是将panel.text() 添加到 hrabel 的代码中,就像我在上面的回答中所做的那样(在此处与 hrabel 的代码分开。
【解决方案2】:

我无法使用plot() 复制您的答案。感谢hrabel 的格子回答。这要归功于他。

ggplot2

library(ggplot2)

fit <- lm(lnedna ~ lncount, data = abundance)


ggplot(data = abundance, aes(x = lncount, y = lnedna))+
geom_point()+
geom_smooth(method = "lm", se = FALSE)+
xlab("Mussel Count by Snorkel Survey")+
ylab("eDNA concentraion (gene sequence/Liter) on log scale")+
ggtitle("Mussel Abundance")+
annotate("text" ,x = 1300, y = 6.13, label = paste("R-squared = ", summary(fit)$r.squared))

格子(来自下面的 hrabel)

lattice::xyplot(lnedna ~ lncount, data = abundance, 
                panel = function(x, y) {
                  lattice::panel.xyplot(x, y)
                  lattice::panel.abline(lm(y ~ x))
                  lattice::panel.text(1300, 6.13, paste("R-squared = ", summary(fit)$r.squared), sep = "")
                },
                xlab = "Mussel Count by Snorkel Survey",
                ylab = "eDNA concentraion (gene sequence/Liter) on log scale", 
                main = "Mussel Abundance")

【讨论】:

  • 谢谢@hrabel 和@InfiniteFlashChess - 我应该做些什么来给你所有的信任吗?
  • 您可以checkmark您认为提供了您想要的答案的用户。请注意,您只能选中一个用户。
  • @Ellen :您也可以投票(点击向上箭头)一个或两个问题。 Some info
猜你喜欢
  • 2011-12-03
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 2014-06-21
  • 2015-10-31
相关资源
最近更新 更多