Home > Mobile >  R plotly - wrong display when overlaying semi-transparent objects (in my case geom_ribbon)
R plotly - wrong display when overlaying semi-transparent objects (in my case geom_ribbon)

Time:06-30

When I run the following lines:

library(plotly)

set.seed(1)
x <- 1:100
y1 <- sin(seq(1, 2*pi, length.out = 100))
y2 <- cos(seq(1, 2*pi, length.out = 100))
plotdata <- rbind(data.frame(x=x, fct = replicate(100, 'sin'), y=y1, lower = (y1 runif(100, -1, -0.5)), upper = (y1 runif(100, 0.5, 1))),
                  data.frame(x=x, fct = replicate(100, 'cos'), y=y2, lower = (y2 runif(100, -1, -0.5)), upper = (y2 runif(100, 0.5, 1))))

p <-
  ggplot(plotdata, aes(group = fct))  
  geom_line(aes(y=y, x=x))  
  geom_ribbon(aes(x=x, ymin=lower, ymax=upper), alpha = 0.3)
p

ggplotly(p)

pdisplays as expected: enter image description here Which is not the case with ggplotly(p) enter image description here Any advice on how to display properly semi-transparent overlaying parts would be welcome.

CodePudding user response:

You can do it natively with plot_ly()

library(plotly)
set.seed(1)
x <- 1:100
y1 <- sin(seq(1, 2*pi, length.out = 100))
y2 <- cos(seq(1, 2*pi, length.out = 100))
plotdata <- rbind(data.frame(x=x, fct = replicate(100, 'sin'), y=y1, lower = (y1 runif(100, -1, -0.5)), upper = (y1 runif(100, 0.5, 1))),
                  data.frame(x=x, fct = replicate(100, 'cos'), y=y2, lower = (y2 runif(100, -1, -0.5)), upper = (y2 runif(100, 0.5, 1))))

plot_ly() %>% 
  add_ribbons(data = subset(plotdata, fct == "sin"), 
              x=~x, 
              ymin=~lower, 
              ymax=~upper, 
              fillcolor="rgba(128,128,128,.2)", 
              line = list(color="transparent"), 
              showlegend=FALSE) %>% 
  add_ribbons(data = subset(plotdata, fct == "cos"), 
              x=~x, 
              ymin=~lower, 
              ymax=~upper, 
              fillcolor="rgba(128,128,128,.2)", 
              line = list(color="transparent"), 
              showlegend=FALSE) %>% 
  add_lines(data = subset(plotdata, fct == "sin"), 
              x=~x, 
              y=~y,
              line = list(color="black"), 
            showlegend=FALSE) %>% 
  add_lines(data = subset(plotdata, fct == "cos"), 
            x=~x, 
            y=~y,
            line = list(color="black"), 
            showlegend=FALSE)

Created on 2022-06-29 by the reprex package (v2.0.1)

My answer benefitted from this answer to a related, but different question.

  • Related