Rplotly:如何设置第二个图例组的图例方向?

发布于 2025-01-11 13:40:54 字数 1021 浏览 2 评论 0原文

我知道我可以使用 layout(legend = list(orientation = 'h')) 设置绘图的图例方向。但是,如果我在图中包含第二个图例组,我怎样才能将该图例组的方向设置为水平。这是一个简单的例子。您可以看到第一个图例组是水平的,但第二个图例组是垂直的。谢谢!

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

图例

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文