Home > Back-end >  Is there a good method to draw a trend line?
Is there a good method to draw a trend line?

Time:07-04

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.

CodePudding user response:

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))

CodePudding user response:

Try this

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

Plot

CodePudding user response:

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)

  • Related