1

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?

1 Answer 1

1

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")))))
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, no, that is not the hard part; the hard part is to make a button that colors the points by a covariate. I did manage to figure it out, eventually, but it is quite an ugly hack and I will post the answer here if there is not a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.