8

I have some data and two categories for each datapoint "symbol" and "name". I am plotting the data and mapping the categories with different symbols and colors as seen below.

data <- data.frame(
        x= c(1,2,3,4,5,6,7,8),
        y = c(10,11,10,12,11,9,8,13), 
        symbol = c("invalid", "valid", "invalid",
                   "valid", "valid", "valid","valid", "valid"),
        name = c("A", "B", "B", "A", "A", "A", "B", "B"))


plot_ly(data) %>%
add_markers(y = ~y,  x = ~x,  symbol = ~symbol, color = ~name,
            symbols = c(4, 27),
            mode = 'markers',
            #split = ~name,
            colors = c("red", "navy"))

enter image description here

The legend is very clumsy.

I want to merge the two legend entries with the cross and have a neutral color for the cross in the legend, like this:

enter image description here

I tried using the split parameter, but that did not help.

1 Answer 1

7

You can do it in 3 steps:

  1. Plot the valid data
  2. Plot all invalid data grey and with legend
  3. Overplot all invalid data in colors without legend

Code:

data$labels <- data$name
levels(data$labels) <- c("A", "B", "invalid")

plot_ly(data, colors = c("red", "navy", "grey")) %>%
  add_markers(y = ~y,  x = ~x, color = ~labels,
              data = data[data$symbol == "valid",],
              symbols = 4,
              mode = 'markers') %>%
  add_markers(y = ~y,  x = ~x,  symbol = ~symbol, 
              color = factor("invalid", levels = c("A", "B", "invalid")),
              data = data[data$symbol == "invalid",],
              symbols = 27,
              mode = 'markers',
              name = 'invalid',
              legendgroup = factor("invalid", levels = c("A", "B", "invalid"))) %>%
  add_markers(y = ~y,  x = ~x,  symbol = ~symbol, color = ~labels,
              data = data[data$symbol == "invalid",],
              symbols = 27,
              mode = 'markers',
              legendgroup = factor("invalid", levels = c("A", "B", "invalid")),
              showlegend = FALSE)

Result:

enter image description here

4
  • it throws me this error: Error in unique.default(x) : unique() applies only to vectors
    – MLavoie
    Commented Aug 22, 2018 at 13:22
  • I also cannot reproduce the plot with your code. Maybe ~labels should be ~name. If I do that I get your plot, but the legend shows "invalid invalid invalid" as label for the cross. Yet the idea is nice. Is there a way to let the label click interaction make sense? So that if one clicks the cross label, all invalid data points are hidden.
    – PascalIv
    Commented Aug 22, 2018 at 13:25
  • Sorry, I forgot to put the variable labels, I've already added it. Thank you! Commented Aug 22, 2018 at 14:50
  • With legendgroup you can delete invalid records, but when you filter A or B, the invalids of this category are kept, this is the closest solution that I can think of to what you need, I hope it works for you. Commented Aug 23, 2018 at 6:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.