-1

How to add a trendline in echarts4r line plots. In the example df , i can add a red trendline in ggplot2. How can i do the same with echart4r ? . Sample data and code are below.

# Required
library(Kendall)
library(zyp)
library(tidyverse)
library(echarts4r)

df <- structure(list(yr = c(1991, 1992, 1993, 1994, 1995, 1996, 1997, 
                      1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
                      2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 
                      2020, 2021, 2022, 2023), val = c(-14.46, 9.92, -0.43, 0.22, -16.77, 
                                                       32.62, 6.78, -15.08, -5.26, 1.89, 3.17, -7.86, 3.55, -21.12, 
                                                       1.53, 2.57, 11.36, -9.24, -13.61, -10.89, -7.72, 6.53, 25.68, 
                                                       28.39, 12.06, -4.89, 4.51, -0.27, 4.65, -20.68, -8.02, 5.99, 
                                                       19.12)), row.names = c(NA, -33L), class = c("tbl_df", "tbl", 
                                                                                                   "data.frame"))

Calcualte trend and intercept ( MK) to get slope and intercept

trnd <- zyp.sen(val ~ yr, df)
df$trn <-  trnd$coeff[[2]]
df$incpt <-  trnd$coeff[[1]]
xs = c(min(df$yr), max(df$yr))
trn_slp = c(unique(df$incpt), unique(df$trn))
ys = cbind(1, xs) %*% trn_slp

trend plot using ggplot2. works fine.

trnd_plt<-
  ggplot(data = df, aes(x = yr, y = val)) +
  geom_hline(yintercept = 0,
             color = "gray50",
             linewidth = 1) +
  geom_line(color = "blue", linewidth = 1) +
  geom_point(color = "blue", size = 2) +
  geom_segment(aes(
    x = xs[[1]],
    xend = xs[[2]],
    y = ys[[1]],
    yend = ys[[2]]
  ),
  color = "red",
  linewidth = 1)
trnd_plt

Plot using echarts4r .

How to add a red trend line with slope and intercept here?

df$yr_chr <- as.character(df$yr) # else echarts plot x-axis from 0 to 2500. 
df %>%
  e_charts(x = yr_chr) %>%
  e_line(serie = val, color="blue")%>% # add a line
  e_scatter(serie = val,color='blue')%>%
  e_legend(show = F) %>%
  e_tooltip() %>%
  e_theme("infographic") %>%
  e_title(text = "test Mann-Kendall trend plot ")
Lily Nature
  • 613
  • 7
  • 18
  • Google found this;: “ There are several statistical functions available, including regression lines and error bars, such as `e_lm(Condo ~ SingleFamily, color = "green")` to add a linear regression line.” – IRTFM Jul 20 '23 at 21:30

1 Answers1

0

To the best of my knowledge there is no direct way. We have to calculate the values for the trend line. We could to it with lm():

# To calculate the values for the trend line

model <- lm(val ~ yr, data = df)

df$trendline <- model$coefficients[1] + model$coefficients[2] * df$yr


library(echarts4r)

df$yr_chr <- as.character(df$yr)

df %>%
  e_charts(x = yr_chr) %>%
  e_line(serie = val, name = "Original", symbol = 'circle', smooth = F) %>%
  e_line(serie = trendline, name = "Trendline", smooth = F) %>%
  e_legend(show = T) %>%
  e_tooltip() %>%
  e_theme("infographic") %>%
  e_title(text = "test Mann-Kendall trend plot with trendline")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66