Rplotly:如何设置第二个图例组的图例方向?
我知道我可以使用 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论