如何将Plotly仪表图并排绘制?

6

看起来plotly仪表图与subplot不兼容,因为最终两个仪表图会重叠在一起。

library(plotly)

fig1 <- plot_ly(
  domain = list(x = c(0, 1), y = c(0, 1)),
  value = 270,
  title = list(text = "Speed"),
  type = "indicator",
  mode = "gauge+number") 
fig1 <- fig1 %>%
  layout(margin = list(l=20,r=30))

fig1

fig2 <- plot_ly(
  domain = list(x = c(0, 1), y = c(0, 1)),
  value = 50,
  title = list(text = "Speed"),
  type = "indicator",
  mode = "gauge+number") 
fig2 <- fig2 %>%
  layout(margin = list(l=20,r=30))

fig2

fig <- subplot(fig1,fig2)
fig
1个回答

5

在域中定义的x和y值将覆盖任何其他布局选项。

使用x和y定义来指定仪表的位置:

library(plotly)

fig1 <- plot_ly(
  domain = list(x = c(0, 0.45), y = c(0, 1)),
  value = 270,
  title = list(text = "Speed"),
  type = "indicator",
  mode = "gauge+number") 
fig1 <- fig1 %>% layout(margin = list(l=20,r=30))

fig2 <- plot_ly(
  domain = list(x = c(0.55, 1), y = c(0, 1)),
  value = 50,
  title = list(text = "Speed"),
  type = "indicator",
  mode = "gauge+number") 
fig2 <- fig2 %>% layout(margin = list(l=20,r=30))

fig <- subplot(fig1, fig2)
fig

enter image description here


你知道为什么在subplot中使用测量工具时不会自动调整大小,但其他图表会吗?在Plotly的基本subplot示例中,域没有指定(https://plotly.com/r/subplots/)。 - SRL
不,我没有。我和你一样想要学习Plotly的细节。它是一个强大的工具,但也有它自己的小问题。 - Dave2e
这里有一份不错的参考资料,https://plotly.com/python/indicator/,它是关于 Python 的(R 页面无法访问),提供了一个基本指南。 - Dave2e

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接