趋势线未显示在图中(baseR)。

huangapple go评论79阅读模式
英文:

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

趋势线未显示在图中(baseR)。

英文:

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 &lt;- c(2.1, 1.2, 1.5, 1.1, 2, 1.4)

abline(lm(gem_wortels[-1] ~ log10(IAA_M[-1])),
       lty = 3, 
       col = &#39;red&#39;)

趋势线未显示在图中(baseR)。

huangapple
  • 本文由 发表于 2023年5月15日 03:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定