January
January

Reputation: 17090

Menu or buttons to select by which variable style the plot in R plotly

I would like to produce a plotly graph in which it is possible to click on a button to select which variable is used to color the markers.

Now, the following example produces a plotly plot with two buttons, except that they don't work:

df <- data.frame(
  x=rnorm(10),
  y=rnorm(10))

group1 <- sample(c("A", "B"), 10, replace=TRUE)
group2 <- sample(c("X", "Y"), 10, replace=TRUE)

p <- plot_ly(data=df, type="scatter", mode="markers", x=~ x, y=~ y)

p %>% layout(
  updatemenus=list(list(
    type="buttons",
    y=.8,
    buttons=list(
      list(method="restyle",
           args=list("color", group1),
           label="Group 1"),
      list(method="restyle",
           args=list("color", group2),
           label="Group 2")
    )
  )))

What I could probably do is to add different markers with different colors and use buttons to toggle their visibility only. However, I already use that trick for showing different data sets on the same plot and adding each data set with all the different options would unnecessarily duplicate the data.

Is there a simpler solution?

Upvotes: 1

Views: 915

Answers (1)

sambold
sambold

Reputation: 817

It took me some time to figure it out, but I think your color argument is wrong. Instead you should use marker.color. Also the group variable should contain colours (like "red" or RGB) ... When I tried to use a color variable with multiple colors, only one of them was displayed. So I think I can only give you some hints here ... maybe I find some more time to experiment later :)

dat <- dplyr::tibble(X=rnorm(10),
                     Y=rnorm(10))

plotly::plot_ly() %>%
  plotly::add_trace(data=dat,
                    x=~X,
                    y=~Y,
                    type="scatter",
                    mode="markers",
                    color="yellow") %>%
  plotly::layout(
    updatemenus=list(
      list(
        type = "buttons",
        y = 0.8,
        buttons = list(
          list(method = "restyle",
               args = list("marker.color","blue"),
               label = "Blue"),
          list(method = "restyle",
               args = list("marker.color","red"),
               label = "Red")))))

EDIT: I found some time to experiment a little more and think I found a not-so-hacky solution adding the color as list

"%>%" <- magrittr::"%>%"

dat <- dplyr::tibble(X=rnorm(10),
                     Y=rnorm(10),
                     COL1=rep(c("1","2"),5),
                     COL2=c(rep("1",8),rep("2",2)))

plotly::plot_ly() %>%
  plotly::add_trace(data=dat,
                    x=~X,
                    y=~Y,
                    type="scatter",
                    mode="markers") %>%
  plotly::layout(
    updatemenus=list(
      list(
        type = "buttons",
        y = 0.8,
        buttons = list(
          list(method = "restyle",
               args = list("marker.color",list(~COL1)),
               label = "COL1"),
          list(method = "restyle",
               args = list("marker.color",list(~COL2)),
               label = "COL2")))))

Upvotes: 1

Related Questions