Skip to content

Setting anchor = "free" in R subplot() causes the graph to disappear #1768

Open

Description

@duncanrellis

I'm trying to build a subplot of stacked scatter plots, where there are multiple axes on each scatter plot.

Below is an example of the type of plot. To position the yaxis side-by-side, I have layout(yaxis(anchor = "free")) set am using position to place the axis.

image

All my attempts to set anchor = "free" in subplots left my plot space completely blank. Some testing revealed that even a subplot constructed from a single plot will not display if anchor="free"

Please see below REPREX.

require(plotly)
df <- data.frame(x = 1:10, y = 1:10, y2 = 10:19)
plt <- plot_ly(data = df) %>%
  add_trace(x = ~x, y = ~y,
            mode = "markers", type = "scatter",
            yaxis = "y") %>%
  add_trace(x = ~x, y = ~y2,
            mode = "markers", type = "scatter",
            yaxis = "y2") %>%
  layout(
    xaxis = list(
      domain = c(.12, .88)
    ),
    yaxis = list(
      title = "y_1"
    ),
    yaxis2 = list(
      title = "y_2",
      anchor = "free",
      position = .10,
      overlaying = "y"
    ),
    margin = list(
      pad = 60
    )
  )
subplot(plt)

Activity

  1. cpsievert commented on May 19, 2020

    @cpsievert
    Collaborator

    I think this is a plotly.js bug...that R code generates this fatal JS error:

    Uncaught TypeError: t.match is not a function
        at r.cleanId (plotly-latest.min.js:61)
        at Object.r.cleanLayout (plotly-latest.min.js:61)
        at Object.r.plot (plotly-latest.min.js:61)
        at Object.renderValue (plotly.js:162)
        at htmlwidgets.js:653
        at Array.forEach (<anonymous>)
        at forEach (htmlwidgets.js:55)
        at htmlwidgets.js:576
        at Array.forEach (<anonymous>)
        at forEach (htmlwidgets.js:55)
  2. r2evans commented on May 19, 2020

    @r2evans

    @cpsievert, I don't have experience with plotly outside of R, do you know if this is reproducible in a non-R context? (I understand your point that the error is coming from plotly and not R.) In your experience, is plotly (the organization) responsive to bugs like that?

  3. cpsievert commented on May 19, 2020

    @cpsievert
    Collaborator

    I just filed an issue here plotly/plotly.js#4844. Please follow that thread for updates (then, if and when it gets fixed in plotly.js, you can poke me about updating plotly.js in the R package :) )

  4. cpsievert commented on May 19, 2020

    @cpsievert
    Collaborator

    Oh actually, this is not a plotly.js problem. And now that I look closer, why do you need subplot(plt) instead of plt?

  5. duncanrellis commented on May 20, 2020

    @duncanrellis
    Author

    Oh actually, this is not a plotly.js problem. And now that I look closer, why do you need subplot(plt) instead of plt?

    For this particular example I don't, I was just trying to provide a minimal set of code that didn't function. My use case that uncovered this issue was trying to create a subplot of two "stacked" plots using subplot()

    Try 1.

    • This approach works, but the axis are overlapped.
    require(plotly)
    df <- data.frame(x = 1:10, y = 1:10, y2 = (10:19)^2)
    plt1 <- plot_ly(data = df) %>%
      add_trace(x = ~x, y = ~y,
                mode = "markers", type = "scatter",
                yaxis = "y") %>%
      add_trace(x = ~x, y = ~y2,
                mode = "markers", type = "scatter",
                yaxis = "y2") %>%
      layout(
        xaxis = list(
          domain = c(.12, .88)
        ),
        yaxis = list(
          side = "left",
          title = "y_1"
        ),
        yaxis2 = list(
          side = "left",
          title = "y_2",
          ## anchor = "free",
          ## position = .10,
          overlaying = "y"
        ),
        margin = list(
          pad = 60
        )
      )
    
    plt2 <- plot_ly(data = df) %>%
      add_trace(x = ~x, y = ~y,
                mode = "markers", type = "scatter",
                yaxis = "y") %>%
      add_trace(x = ~x, y = ~y2,
                mode = "markers", type = "scatter",
                yaxis = "y2") %>%
      layout(
        xaxis = list(
          domain = c(.12, .88)
        ),
        yaxis = list(
          side = "left",
          title = "y_1"
        ),
        yaxis2 = list(
          side = "left",
          title = "y_2",
          ## anchor = "free",
          ## position = .10,
          overlaying = "y3"
        ),
        margin = list(
          pad = 60
        )
      )
    
    subplot(plt1, plt2, nrows = 2)
    
    

    ##Try 2

    • When you un-comment out the anchor = "free" argument the entire plot disappears, regardless of where position is set.
    Require(plotly)
    df <- data.frame(x = 1:10, y = 1:10, y2 = (10:19)^2)
    plt1 <- plot_ly(data = df) %>%
      add_trace(x = ~x, y = ~y,
                mode = "markers", type = "scatter",
                yaxis = "y") %>%
      add_trace(x = ~x, y = ~y2,
                mode = "markers", type = "scatter",
                yaxis = "y2") %>%
      layout(
        xaxis = list(
          domain = c(.12, .88)
        ),
        yaxis = list(
          side = "left",
          title = "y_1"
        ),
        yaxis2 = list(
          side = "left",
          title = "y_2",
          anchor = "free",
          position = .10,
          overlaying = "y"
        ),
        margin = list(
          pad = 60
        )
      )
    
    plt2 <- plot_ly(data = df) %>%
      add_trace(x = ~x, y = ~y,
                mode = "markers", type = "scatter",
                yaxis = "y") %>%
      add_trace(x = ~x, y = ~y2,
                mode = "markers", type = "scatter",
                yaxis = "y2") %>%
      layout(
        xaxis = list(
          domain = c(.12, .88)
        ),
        yaxis = list(
          side = "left",
          title = "y_1"
        ),
        yaxis2 = list(
          side = "left",
          title = "y_2",
          anchor = "free",
          position = .10,
          overlaying = "y3"
        ),
        margin = list(
          pad = 60
        )
      )
    
    subplot(plt1, plt2, nrows = 2)
    
    

    In my testing I found that even if only a single plot obj was added to the subplot() function, the plot would be blank, which is why I constructed the REPREX in the above manner.

    Hope that's clear.

  6. duncanrellis commented on May 20, 2020

    @duncanrellis
    Author
    • Not quite related, but I find it confusing from a plot construction perspective that the overlaying argument on the second plot is indexed at a "global" level, that is, it must be set to overlaying = "y3" instead of overlaying = "y" to appear in the right place.

    Not sure if that's a design feature, or unintended consequence of subplot()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions