有很好的方法来绘制趋势线吗?

发布于 2025-02-13 03:33:51 字数 1892 浏览 1 评论 0原文

我有一个称为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:

The red trend line

Method 1

plot(b, values,
xlab= "Year", ylab = "Values (%)", main="Profit on equity", pch=19, type="line")
grid()
box()

Result:
Value_1: a line

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:
Value_2: trendline with different directions.

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:
Value_3: dots and no line.

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:
Value_4: dots and no line.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2025-02-20 03:33:51

我认为您正在寻找这个。

plot(dat[c('b', 'values')], type='l', 
     main='Profit on equity', xlab='Year', ylab='Values (%)')
grid()
abline(lm(values ~ b, dat), col=2, lwd=2)


数据:

dat <- structure(list(b = c(2015, 2016, 2017, 2018, 2019, 2020), values = c(0.1239, 
0.1147, 0.1151, 0.1195, 0.1143, 0.0856)), class = "data.frame", row.names = c(NA, 
-6L))

I think you are looking for this.

plot(dat[c('b', 'values')], type='l', 
     main='Profit on equity', xlab='Year', ylab='Values (%)')
grid()
abline(lm(values ~ b, dat), col=2, lwd=2)

![enter image description here


Data:

dat <- structure(list(b = c(2015, 2016, 2017, 2018, 2019, 2020), values = c(0.1239, 
0.1147, 0.1151, 0.1195, 0.1143, 0.0856)), class = "data.frame", row.names = c(NA, 
-6L))
圈圈圆圆圈圈 2025-02-20 03:33:51

尝试此

plot(b, values, xlab= "Year", ylab = "Values (%)", main="Profit on equity", pch=19)
abline(lm(values~b))

Try this

plot(b, values, xlab= "Year", ylab = "Values (%)", main="Profit on equity", pch=19)
abline(lm(values~b))

Plot

泼猴你往哪里跑 2025-02-20 03:33:51

这是ggplot2方式。

  • 数据的线图很简单;
  • 公式参数继承xy美学。比较与您的方法
c(2015, 2016, 2017, 2018, 2019, 2020) -> b 
c(0.1239, 0.1147, 0.1151, 0.1195, 0.1143, 0.0856) -> values
dat <- data.frame(b, values)

library(ggplot2)

ggplot(dat, aes(b, values)) +
  geom_line() +
  geom_smooth(formula = y ~ x, method = lm, se = FALSE, color = "red", size = 1.5) +
  labs(x = "Year", y = "Values (%)") +
  theme_bw()

4 /reprex.tidyverse.org“ rel =“ nofollow noreferrer”> reprex软件包 (v2.0.1)


编辑

要将绘图标题放在绘图的中间,theme_bw必须在设置theme(plot.title =。)之前出现。 。

ggplot(dat, aes(b, values)) +
  geom_line() +
  geom_smooth(formula = y ~ x, method = lm, se = FALSE, color = "red", size = 1.5) +
  labs(x = "Year", y = "Values (%)") +
  ggtitle("Return on Equity") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))

Here is a ggplot2 way.

  • The line plot of the data is straightforward;
  • The formula argument inherits the x and y aesthetics. Compare to your method 4.
c(2015, 2016, 2017, 2018, 2019, 2020) -> b 
c(0.1239, 0.1147, 0.1151, 0.1195, 0.1143, 0.0856) -> values
dat <- data.frame(b, values)

library(ggplot2)

ggplot(dat, aes(b, values)) +
  geom_line() +
  geom_smooth(formula = y ~ x, method = lm, se = FALSE, color = "red", size = 1.5) +
  labs(x = "Year", y = "Values (%)") +
  theme_bw()

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 setting theme(plot.title = .).

ggplot(dat, aes(b, values)) +
  geom_line() +
  geom_smooth(formula = y ~ x, method = lm, se = FALSE, color = "red", size = 1.5) +
  labs(x = "Year", y = "Values (%)") +
  ggtitle("Return on Equity") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文