简体   繁体   中英

plotly in R : feeding add_trace with data from a list() object

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM