R可视化和ggplot2

《R数据可视化手册》学习笔记5---散点图(9)拟合曲线

2023-10-01  本文已影响0人  RSP小白之路

写在前面。


拟合曲线

拟合线

如何添加回归模型拟合线呢?

运行stat_smooth函数并设定method=lm

示例数据heightweight数据集:

ggplot(data = heightweight, aes(x =ageYear , y = heightIn))+
  geom_point() +
  stat_smooth(method = lm)
ggplot(data = heightweight, aes(x =ageYear , y = heightIn))+
  geom_point() +
  stat_smooth(method = lm, se = FALSE)

更多的添加拟合曲线的知识查看Rstat_smooth相关文档。

另外,还可以向图中添加系数,其实就是添加了一个标签,在添加前计算出来。不赘述了。


以上。

上一篇 下一篇

猜你喜欢

热点阅读