r - 在R中的图形上绘制对数曲线

标签 r ggplot2 regression curve-fitting

我有以下数据集,绘制时具有曲线关系

Fish.species.richness   Habitat.Complexity  log.habitat
17  0.6376  -0.1954858
13  0.2335  -0.6317131
30  0.2866  -0.5427238
20  0.3231  -0.4906630
22  0.1073  -0.9694003
25  0.2818  -0.5500590
2   0.2182  -0.6612448
4   0.0189  -1.7246886
19  0.2960  -0.5287083
25  0.5507  -0.2590849
29  0.2689  -0.5704900
21  0.6286  -0.2016602
18  0.1557  -0.8078509
24  0.6851  -0.1642460
30  0.5059  -0.2959353
32  0.4434  -0.3532043
29  0.3585  -0.4455108
32  0.5920  -0.2276783

当我记录 x 轴并进行线性回归以找到截距和斜率时,我能够添加一条适合数据的线:

summary(lm(Fish.species.richness~log.habitat,data=three))

plot(three$log.habitat,
 three$Fish.species.richness,
 xlab='Log Habitat Complexity',
 ylab='Fish Species Richness')
abline(29.178,13.843)

但是,当我随后进行曲线回归并尝试绘制曲线时,它与数据不符,我哪里出错了?

mod.log<-lm(Fish.species.richness~log(Habitat.Complexity),data=three)

plot(three$Habitat.Complexity,
 three$Fish.species.richness)
abline(mod.log)

最佳答案

使用ggplot2:

ggplot(three, aes(Habitat.Complexity, Fish.species.richness))+
  geom_point(shape = 1) + stat_smooth(method = "lm", formula = y ~ log(x))

enter image description here

关于r - 在R中的图形上绘制对数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226150/

相关文章:

python - Keras RNN 回归输入维度和架构

python - 在不组合特征的情况下运行多项式回归

r - 在 R 中使用 ifelse

r - 如何在ggplot直方图中添加均值和众数?

r - 如何在不创建摘要数据框的情况下在 ggplot2 中标记堆积条形图?

r - stat_summary计算失败, 'what'必须是字符串或函数

r - 计算MSE : why are these two ways giving different results?

r - 如何检索...的内容作为调用列表?

r - 如何在 Shiny 的情况下创建 On load 事件或默认事件?

r - 对来自与 rowname 匹配的两个数据帧的数据求和