首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将abline添加到R中的散点图时出错

将abline添加到R中的散点图时出错
EN

Stack Overflow用户
提问于 2017-12-23 06:25:54
回答 2查看 501关注 0票数 1

散点图

代码语言:javascript
复制
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")

添加拟合线

代码语言:javascript
复制
abline(lm(lnedna ~ lncount))

这是我在尝试添加回归行时不断得到的错误: error in eval(expr,envir,enclos):找不到对象'lnedna‘

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

EN

回答 2

Stack Overflow用户

发布于 2017-12-23 06:34:20

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

代码语言:javascript
复制
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?

票数 2
EN

Stack Overflow用户

发布于 2017-12-23 07:07:30

我不能用plot()重现你的答案。感谢hrabel提供的网格答案。这要归功于他。

ggplot2

代码语言:javascript
复制
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 )

代码语言:javascript
复制
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")

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47948046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档