plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Shapes/images don't accumulate with multiple calls to layout()

Open cpsievert opened this issue 7 years ago • 5 comments

line1 <- list(
  type = "line",
  x0 = 0,
  x1 = 1,
  y0 = 0.5,
  y1 = 0.5,
  xref = "paper",
  yref = "paper",
  line = list(color = "#C3C4C6")
)

line2 <- list(
  type = "line",
  x0 = 0,
  x1 = 1,
  y0 = 0.75,
  y1 = 0.75,
  xref = "paper",
  yref = "paper",
  line = list(color = "#C3C4C6")
)

plotly_empty() %>%
  layout(shapes = line1) %>%
  layout(shapes = line2) 

cpsievert avatar Sep 05 '18 20:09 cpsievert

This also appears to be a problem when accumulating shapes in subplot()?

library(dplyr)
library(plotly)
set.seed(123)

dat <- data.frame(
  a = rnorm(100), b = rnorm(100), c = rnorm(100),
  class = sample(c("g1", "g2", "g3"), 100, replace = TRUE),
  grp = sample(c("n", "o", "p", "q", "r"), 100, replace = TRUE))


plots <- dat %>%
  group_by(class) %>%
  do(plot = {
    plot_ly(., x = ~a, y = ~b, legendgroup = ~grp, 
            showlegend = .$class[1] == "g1") %>%
      add_markers(color = ~grp) %>%
      layout(
          xaxis = list(showzeroline = FALSE), 
          yaxis = list(showzeroline = FALSE),
          shapes = list(
              type = "line",
              x0 = 0,
              x1 = 0, 
              y0 = 0, 
              y1 = 1,
              xref = "paper",
              yref = "paper"
          ),
          list(
              type = "line",
              x0 = 0,
              x1 = 0, 
              y0 = 0, 
              y1 = 1,
              xref = "paper",
              yref = "paper"
          ))
  })
subplot(plots, nrows = 2, shareX = TRUE, shareY = TRUE)

Related https://community.rstudio.com/t/adding-borders-to-all-subplots-in-plotly-when-sharing-axes/30289

cpsievert avatar May 09 '19 15:05 cpsievert

@cpsievert Hi Carson, do you know if having multiple subplot shapes is fixed or will be fixed anytime soon? I'd like to have two rectangle shape (one around the subplot title, and one in the plot itself), but I can only add one of them. This is the case for just one plot as well. plot <- plot %>% add_markers(x=~X, y=~Y, showlegend=F) %>% add_lines(x=~X, y=~Y,showlegend=F) shape_rec1 <- list(type = "rect", x0 = 0, x1 = 1.1, xref = "paper", y0 = -1,y1 = 18, yanchor = 1, yref = "paper",ysizemode = "pixel", line = list(color = "grey") ) shape_rec2 <- list( list(type = "rect", fillcolor = "#fffdc9", line = list(color = "#fcf7b6"), opacity = 0.5, x0 = -45, x1 = -15, xref = "x", y0 = 1.5, y1 = 6, yref = "y")) facet_title <- "plotly multiple shapes" plot %>% add_annotations( text = sprintf("<b>%s</b>",facet_title), x = 0.5, y = 1.1, yref = "paper", xref = "paper", yanchor= "bottom",showarrow = FALSE,font = list(size = 14) ) plot <- plot %>% add_annotations( text = sprintf("<b>%s</b>",facet_title), x = 0.5, y = .98, yref = "paper",xref = "paper", yanchor = "bottom", showarrow = FALSE, font = list(size = 14) ) Only one of the shapes gets added: plot %>% layout(shapes=list(shape_rec1,shape_rec2)) %>% config(editable=TRUE)

mehrnoushmalek avatar Nov 14 '22 14:11 mehrnoushmalek

I'm having the same problem. Has this bug been worked out? Is this still the workaround?

https://forum.posit.co/t/drawing-lines-from-layouts-doesnt-work-on-ggplotly/54116/6

Thanks, David

dbcrow avatar Jul 10 '24 19:07 dbcrow

I have the same bug, 5 hours making a small layout line in plotly is not time well spent, but thanks for the link, it works setting it directly :)

Roleren avatar Feb 27 '25 09:02 Roleren

By the way, the way described in the link above is unsafe, i.e. try to put that plot in a subplot and it will crash with the error: Error in FUN(X[[i]], ...) : 'options' must be a fully named list, or have no names (NULL)

What does work is to combine with c().

p$x$layout$shapes <- 
c(p$x$layout$shapes, 
   list(list(
      type = "line", 
      line = list(color = "gray", dash = "dot"),
      x0 = 1, 
      x1 = 1,
      y0 = 0,
      y1 = 1,
      yref = "paper"
    )) # double list to force non named subset, else subplot fails
  )
p

Now subplot will work too.

Roleren avatar Feb 27 '25 09:02 Roleren