英文:
Trendline does not show up in plot (baseR)
问题
I'm trying to create a trendline on a scatterplot using baseR. I used the abline()
function. Upon executing, I did not encounter an error, but the trendline does not show up in the plot. Could it be because I plotted on a logarithmic scale?
我正在尝试在散点图上使用baseR创建趋势线。我使用了abline()
函数。执行时,我没有遇到错误,但趋势线未显示在图中。这可能是因为我在对数刻度上绘制了图吗?
英文:
I'm trying to create a trendline on a scatterplot using baseR. I used the abline()
function. Upon executing, I did not encounter an error, but the trendline does not show up in the plot. Could it be because I plotted on a logarithmic scale?
IAA_M<-c(0, 1E-3, 1E-5, 1E-7, 1E-9, 1E-11)
gem_wortels<-c(7.71, 2.275, 6.34285, 4.71666, 8.36666, 4.822222)
plot(
IAA_M,
gem_wortels,
ylim = range(c(
gem_wortels - stdev_wortels, gem_wortels + stdev_wortels
)),
pch = 19,
xlab = "[IAA] (M)",
ylab = "gemiddelde wortellengte (cm)",
log = 'x'
)
arrows(
IAA_M,
gem_wortels - stdev_wortels,
IAA_M,
gem_wortels + stdev_wortels,
length = 0.05,
angle = 90,
code = 3
)
abline(gem_wortels ~ IAA_M,
data_wortelgroei_regressie,
lty = 3,
col = 'red')
答案1
得分: 3
你需要为abline
提供一个lm
模型,而不仅仅是一个裸公式。然而,在模型的公式右侧,你需要使用x轴的log10
值。另外,由于x轴变量的第一个值为0,所以需要省略第一行,否则lm
会抛出错误,因为log10(0)
是NaN
。
你的示例没有包括标准偏差的向量,所以我为演示目的创建了一个:
stdev_wortels <- c(2.1, 1.2, 1.5, 1.1, 2, 1.4)
abline(lm(gem_wortels[-1] ~ log10(IAA_M[-1])),
lty = 3,
col = 'red')
英文:
You need to supply an lm
model to abline
, not just a bare formula. However, you will need to use the log10
value of your x axis on the right hand side of the formula in the model. Also, since the first value of your x axis variable is 0, the first row needs to be omitted, otherwise lm
will throw an error since log10(0)
is NaN
.
Your example doesn't include the vector of standard deviations, so I have made one up for demonstration purposes:
stdev_wortels <- c(2.1, 1.2, 1.5, 1.1, 2, 1.4)
abline(lm(gem_wortels[-1] ~ log10(IAA_M[-1])),
lty = 3,
col = 'red')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论