使用ggplot绘制趋势线 [英] Draw a trend line using ggplot

查看:1168
本文介绍了使用ggplot绘制趋势线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2根据我的数据绘制趋势线。

以下是我使用电子表格完成的工作。



但是我只想显示趋势线(黑色线如上图所示),而不是所有的点,因为观察值> 20,000。 b
$ b

所以我试图用ggplot2做同样的事情。

  fig_a < -  ggplot(df1,aes(data_x,data_y))
fig_a + stat_smooth(method = lm )
fig_a + stat_smooth(method = gam)

很显然,可以帮助?

为什么它会提供如此多的线条而不是单一的趋势线? ggplot 脚本


$ b $ ggplot2 备忘单可以很好地概述其功能。



示例

  ggplot(mpg,aes (displ,hwy))+ geom_point()+ geom_smooth(method =lm)


I used ggplot2 to draw a trend line based on my data.

Below is something I've done using spreadsheet.

But I only want to show the trend line (black line as shown in upper plot) rather than all dots as number of observation is > 20,000.

So I tried to do the same thing using ggplot2.

fig_a <- ggplot(df1, aes(data_x, data_y ))
fig_a + stat_smooth(method=lm)
fig_a + stat_smooth(method=gam)

Apparently it does not work well, anyone can help?

Why it gives so many lines rather than single trend line?

解决方案

Add + geom_smooth(method = "lm") to your ggplot script.

The ggplot2 cheat sheet gives you a nice overview of its functions.

Example

ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth(method = "lm")

这篇关于使用ggplot绘制趋势线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆