I tried to use a function with a loop for not having to add manually every trace. But it does not work.
library(tidyverse)
library(plotly)
df <- tibble(year = c("2019", "2020"),
x = c(1, 2),
y = c(3, 5),
z = c(2, 7))
addmore <- function(fig, i){
fig %>% add_trace(y = ~df[[i]], name = colnames(df[i]),
mode = 'lines+markers',
line = list(width = 1)) -> fig
}
# This does not work
fig <- plot_ly(df, x = ~year, y = ~df[[2]],
name = colnames(df[2]), type = 'scatter',
mode = 'lines+markers',
line = list(width = 1))
for (i in 3:4){
addmore(fig, i)
}
fig <- fig %>% layout(title = "Sample",
xaxis = list(title = "Years"),
yaxis = list (title = "Values"))
fig
dom_win your code so we can run it (use thedputfunction)