Servet
Servet

Reputation: 287

Plotly R : How to update style of lines without redrawing (local)

I am trying to locally draw dozens of lines in a shiny app. After the lines are drawn, I would like to be able to change their styles without redrawing?

In the example below, for simplicity, there are only 6 traces (lines). I would like to change the opacity of the first and third lines only, without redrawing anything :

library(plotly)
plot_ly(mtcars, x=mpg, y=wt, group=carb)
style(p = last_plot(), opacity=0.1) #Only modifies first line & complains : You've referenced non-existent traces
style(p = last_plot(), opacity=0.1, traces = 3) #Does not work at all
Warning: You've referenced non-existent traces
Error in p$data[[max(traces)]] : subscript out of bounds

How to use style()?

I searched a lot but could not find any documentation

thanks for your help

regards

Upvotes: 0

Views: 544

Answers (2)

royr2
royr2

Reputation: 2289

Maybe this helps? You should be able to jump into any of the traces and modify / add any property fairly quickly.

library(plotly)
p <- plot_ly(mtcars, x=mpg, y=wt, group=carb)
p <- plotly_build(p)
p$data[[1]]$opacity = 0.1
p$data[[3]]$opacity = 0.1

p

Upvotes: 0

MLavoie
MLavoie

Reputation: 9836

I think style() is intended to work with something like that:

p <- plot_ly(x = seq(0, 8), y = rnorm(8), name = "Blue Trace") %>%
       add_trace(y = rnorm(8), name = "Orange Trace") %>% 
       add_trace(y = rnorm(8), name = "Green Trace")
p
pp <- style(p = last_plot(), opacity=0.1, traces = 1)
pp
ppp <- style(pp = last_plot(), opacity=0.1, traces = 2)
ppp
pppp <- style(ppp = last_plot(), opacity=0.1, traces = 3)
pppp 

Upvotes: 1

Related Questions