Skip to content

Can't replicate plot in plotly 4.5.6 (from plotly 3.6.0) #817

@shamshmk

Description

@shamshmk

I’m having trouble replicating a plot I created in R using plotly 3.6.0.
I recently installed plotly 4.5.6, and I’ve tried various ways to get the plot to work as before. I’m hoping you could give some insight on what I’m missing.

Code in version 3.6.0 :

x1 <- data.frame(x=rnorm(100,2),Name="x1")
x2 <- data.frame(x=rnorm(100,6),Name="x2")
x3 <- data.frame(x=rnorm(100,4),Name="x3")
x<-rbind(x1,x2,x3)
y <- rnorm(300)
df <- data.frame(x,y)

cols <- RColorBrewer::brewer.pal(length(unique(df$Name)), name = "Set1")
m1 <-list(size = 10,
          color = factor(df$Name, labels = cols[1:length(unique(df$Name))]),
          line = list(color = 'rgba(0, 0, 0, .8)', width = 1))

plot_ly(df,x = df$x,y=df$y, type="scatter",mode="markers", xaxis = "x1",
        yaxis = "y1",group = Name, marker = m1, legendgroup = Name) %>% 
add_trace(x = df$x, type="histogram",showlegend = F,xaxis = "x1",
              yaxis = "y2",group = Name, marker = m1, legendgroup = Name) %>% 
  add_trace(y = df$y,  type="histogram",showlegend = F,xaxis = "x2",
              yaxis = "y1",group = Name, marker = m1, legendgroup = Name) %>% 
  layout(
                                   showlegend = TRUE, 
                                   barmode="overlay",
                                   xaxis = list(anchor = 'y', domain = c(0, 0.8)),
                                   yaxis = list(anchor = 'x', domain = c(0, 0.8)),
                                   xaxis2 = list(anchor = 'y', domain = c(0.81, 1)),
                                   yaxis2 = list(anchor = 'x', domain = c(0.81, 1)))

this is the result:
1
I’m able to deselect a “trace” and it will affect the show/hide value for all three plots, for example if I click the blue “x2” this is the results:
2

I’ve tried doing this in plotly 4.5.6

plot_ly(df,x = ~x,y=~y, type="scatter", xaxis = "x1",
        yaxis = "y1",split = ~Name, marker = m1, legendgroup = ~Name) %>% 
  add_histogram(x = ~x, showlegend = F,xaxis = "x1",
            yaxis = "y2",split = ~Name, marker = m1, legendgroup = ~Name) %>% 
  add_histogram(x = ~y, showlegend = F,xaxis = "x2",
            yaxis = "y1",split = ~Name, marker = m1, legendgroup = ~Name) %>% 
  layout(
    showlegend = TRUE, 
    barmode="overlay",
    xaxis = list(anchor = 'y', domain = c(0, 0.8)),
    yaxis = list(anchor = 'x', domain = c(0, 0.8)),
    xaxis2 = list(anchor = 'y', domain = c(0.81, 1)),
    yaxis2 = list(anchor = 'x', domain = c(0.81, 1)))

but the result looked different and the legends would only control the scatter plot:
3

4

I was able to get it to look similar using subplots, but still the legend only controls the scatterplot:

s <- subplot(
  plot_ly(df,x = ~x, type="histogram",showlegend = F, marker = m1,color = ~factor(Name)),
  plotly_empty(), 
  plot_ly(df,x = ~x,y=~y, type="scatter",mode="markers",  marker = m1,color = ~factor(Name)), 
  plot_ly(df,y = ~y,  type="histogram",showlegend = F, marker = m1,color = ~factor(Name)),
  nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), 
  shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE
)
layout(s, showlegend = T, barmode = "overlay")

5

Any help would be greatly appreciated.

Thanks,
Shams

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions