🏥 🏭 Working on the COVID-19 response in Canada? Plotly & the Canadian government can help you and your organization. Learn more and get in touch.

Can you display orthogonal attributes grouped separately in the legend?

I have 2 measures across 5 categories. Say, the speed and acceleration of 5 different runners. The speed and acceleration are on different y-axes due to being different units and the speed is solid and the acceleration is dashed. Each runner gets a different colour.

When I do this in plotly I get 10 items in the legend for each colour x linetype combination and the meaning is lost. In ggplot these attributes would be displayed separately, the 5 colours and the 2 line types, the meaning of the attributes are clear.

Can you display these attributes separately (similar to ggplot) using Plotly?

LegendExample

Note: the actual measures are not speed and acceleration, they’re just examples.

Here’s how far I’ve gotten, suitably annonymised with the speed/acceleration example above

plot_ly() %>%
         # Make it so that all x axes pan together, even in side-by-side plots
  layout(xaxis = list(title = "Speed",
                      matches = "x"),
         # Add the right hand axis
         yaxis2 = list(overlaying = "y",
                       side = "right",
                       title = "Acceleration")) %>%
  # Draw the speed
  add_lines(data = speed_data %>%
              mutate(LineType = factor("Speed",
                                       levels = c("Speed",
                                                  "Acceleration"))),
            x = ~Date,
            y = ~Speed,
            color = ~Runner,
            linetype = ~LineType,
            yaxis = "y") %>%
  # Draw the acceleration
  add_lines(data = acceleration_data%>%
              mutate(LineType = factor("Acceleration",
                                       levels = c("Speed",
                                                  "Acceleration"))),
            x = ~Date,
            y = ~Accel,
            color = ~Runner,
            yaxis = "y2")

I’ve gotten round this by adding extra zero point traces with what I want in the legend. It removes most functionality though.