0
votes

I am trying to create a plot in plot_ly in R that has many lines. I would like to be able to use the selection tool as well. An example is below:

library(plotly)

trace_0 <- rnorm(100, mean = 0)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = 0)
x <- c(1:100)

data <- data.frame(x, trace_0, trace_1, trace_2)

plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines') %>%
  add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines') %>%
  add_trace(y = ~trace_2, name = 'trace 2', mode = 'lines') 

Based on this example, I have two questions in terms of tailoring it to my goal:

1) In the example above, the dimensions of the data was 100 rows and 4 columns. Let's say I had a data frame that was 100 rows and 50 columns. Is there a better method to add each of the new lines (by add_trace), or would I need to do that in a for-loop for 49 lines?

2) Is there a way I could still obtain the "select" option. Sometimes, when using plot_ly, there is a box and lasso selection tool that appears. However, for some reason, I am unable to get that by default with this plot at hand.

If you have any suggestions toward either of these goals, I would greatly appreciate your input! Thank you.

2

2 Answers

0
votes

Try this:

p<-plot_ly(y=trace_0, x=x , evaluate=TRUE)
p<-add_trace(y=trace_1, x=x , evaluate=TRUE)
p<-add_trace(y=trace_2, x=x , evaluate=TRUE)
p

enter image description here

0
votes

You can try:

library(plotly)
library(reshape2)
# some reproducible data
d <- cbind.data.frame(x=1:nrow(iris), iris[,-5])
  x Sepal.Length Sepal.Width Petal.Length Petal.Width
1 1          5.1         3.5          1.4         0.2
2 2          4.9         3.0          1.4         0.2
3 3          4.7         3.2          1.3         0.2
4 4          4.6         3.1          1.5         0.2
5 5          5.0         3.6          1.4         0.2
6 6          5.4         3.9          1.7         0.4
# transform to long format using reshape's melt() function  
d_long <- melt(d, id.vars ="x" )
# plot the lines using the group argument for different traces.
plot_ly(d_long, x=  x, y=value, group= variable, type= "line")

enter image description here

A solution for the second question could be the dragmode. Add one of these values to your plot:

c("zoom", "pan", "select", "lasso", "orbit", "turntable")
plot_ly(d_long, x=  x, y= value, group= variable, type= "line") %>%
  layout(dragmode = "lasso")

Check also the layout reference on the plotly website. But I'm not sure whether the icons

Edit:

The newest plotly version plotly_4.5.2 changed the syntax a little bit. Now you have to specify the type to "scatter" or "scattergl" and the mode to "line" for a line plot. The group argument will be set by color or by group_by(). Unfortunatly there is no "lasso" or "select" function in the line-mode at all. Therfore you have to use the "lines+markers" mode. With the dragmode you can specify which feature is preselected.

plot_ly(d_long, x=  ~x, y= ~value, type = 'scatter', mode = 'lines+markers', color = ~variable)  %>% layout(dragmode="lasso")

enter image description here