'R plotly: how can i set legend orientation for a second legendgroup?

I know I can set the legend orientation of a plotly plot with layout(legend = list(orientation = 'h')). However, if I include a second legendgroup in the plot, how can i also set the orientation of that one to horizontal. Here's a simple example. You can see the first legendgroup is horizontal but the second is vertical. Thanks!

library(plotly)
library(dplyr)


d1 <- data.frame(x = rep(seq(1,10), 2), 
                 y = runif(20), 
                 n = c(rep("Entry 1", 10), rep("Entry 2", 10)))
d2 <- data.frame(x = rep(seq(1,10), 4), 
                 y = runif(40), 
                 n = c(rep("Entry A", 10), rep("Entry B", 10),
                       rep("Entry C", 10), rep("Entry D", 10)))

p <- plot_ly()

p <- p %>%
  add_trace(data = d1, type = "scatter", mode = "lines",
            x = ~x, y = ~y, name = ~n)

p <- p %>%
  add_trace(data = d2, type = "bar",
            x = ~x, y = ~y, name = ~n, 
            legendgroup = "group2")

p <- p %>%
  layout(legend = list(orientation = 'h'))
p

legends



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source