1
votes

I have data that is already stored in a list object. I have several lines to draw one one top of the other. I would like to plot them using plotly WITHOUT converting the data into a data frame. In the code below, the first line draws fine, but the additional lines do not draw with add_trace() :

library(plotly)
d1 = list(x=c(1,2,3,4),y=c(1,2,3,4))
d2 = list(x=c(1,2,3,4),y=c(1,2,3,4)+2)
p<-plot_ly(d1,x=x,y=y)
p<-add_trace(d2,x=x,y=y)
p

giving the error

Error in eval(expr, envir, enclos) : object 'x' not found

any ideas? thanks

PS : by the way I also find it quite strange that plot_ly does not work without x=x, y=y. I know data is stored like that internally in plotly .

1

1 Answers

2
votes

You could try:

d1 = list(x=c(1,2,3,4),y=c(1,2,3,4))
d2 = list(x=c(1,2,3,4),y=c(1,2,3,4)+2)
p<-plot_ly(d1,x=x,y=y) %>% add_trace(x=d2[[1]], y=d2[[2]])
p