有很好的方法来绘制趋势线吗?
我有一个称为df
的数据框架,包括:
列B
是:
(2015 , 2016 , 2017 , 2018 , 2019, 2020) -> b
列值
包括:
(0.1239, 0.1147, 0.1151, 0.1195, 0.1143, 0.0856) -> values
我想按B(年)绘制列列(直线)的趋势线(直线),但是我不能。以下是我尝试过的四个不同的方法:
附件文件中的红线是我需要的:
方法1
plot(b, values,
xlab= "Year", ylab = "Values (%)", main="Profit on equity", pch=19, type="line")
grid()
box()
结果:
这是一条线,但不是趋势线(直线)
方法2:
plot(b~values,
xlab= "Year", ylab = "Value (%)", main="Profit on equity", pch=19)
abline(lm(b~means))
grid()
结果:结果:< /strong>。
结果显示了与方法1相反的列。我不知道如何判断其趋势,但尚不清楚。
方法3
plot(values~b,
xlab= "Year", ylab = "Value (%)", main=" Profit on equity", pch=19)
abline(lm(values~b))
grid()
结果:。
这显示了点,没有趋势线或创建的
方法4
ggplot(df, aes(b, values)) + geom_point() +
geom_abline()
或
ggplot(df, aes(b, values)) + geom_point() +
geom_smooth(method = "lm", formula = values ~ b)
结果:。
这显示了点,没有线或直线创建
结论
,即使我尝试了不同的方式,我也无法创建趋势线。
请帮助我解决这个问题。
注意:实际上,很难认识到数据的趋势,因为它可能会上下,因此趋势线有助于我对数据趋势做出判断。
我期待着您的帮助。
I have a data frame called df
including:
column b
is the years :
(2015 , 2016 , 2017 , 2018 , 2019, 2020) -> b
column values
include :
(0.1239, 0.1147, 0.1151, 0.1195, 0.1143, 0.0856) -> values
I want to draw a trend line (straight line) of column values (values) by column b (years), but I can't. Below are four different methods that I tried :
The red line in the attached file is the one that I need:
Method 1
plot(b, values,
xlab= "Year", ylab = "Values (%)", main="Profit on equity", pch=19, type="line")
grid()
box()
Result:
It is a line but not a trend line (straight line)
Method 2:
plot(b~values,
xlab= "Year", ylab = "Value (%)", main="Profit on equity", pch=19)
abline(lm(b~means))
grid()
Result:.
The result shows the columns it is opposite to method 1. I don't know how to judge its trend as it is not clear.
Method 3
plot(values~b,
xlab= "Year", ylab = "Value (%)", main=" Profit on equity", pch=19)
abline(lm(values~b))
grid()
Result:.
This shows dots and no trend line or line created
Method 4
ggplot(df, aes(b, values)) + geom_point() +
geom_abline()
or
ggplot(df, aes(b, values)) + geom_point() +
geom_smooth(method = "lm", formula = values ~ b)
Results:.
This shows dots and no line or straight line created
Conclusion
I can't create a trend line as I wanted even though I have tried different ways.
Please help me to solve this issue.
Note: In practice, it is hard to recognise the trend of data as It may up and down therefore the trend line helps me make a judgement of data trend.
I look forward to receiving your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找这个。
数据:
I think you are looking for this.
Data:
尝试此
Try this
这是
ggplot2
方式。x
和y
美学。比较与您的方法。
4 /reprex.tidyverse.org“ rel =“ nofollow noreferrer”> reprex软件包 (v2.0.1)
编辑
要将绘图标题放在绘图的中间,
theme_bw
必须在设置theme(plot.title =。)
之前出现。 。Here is a
ggplot2
way.x
andy
aesthetics. Compare to your method 4.Created on 2022-07-03 by the reprex package (v2.0.1)
Edit
To put the plot title in the middle of the plot, the
theme_bw
must come before settingtheme(plot.title = .)
.