使用Plotly子图将标题添加到每个y轴

时间:2019-06-12 11:11:13

标签: r plotly

让我们考虑以下情节:

p1 <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "Size",
  type = "bar")

p2 <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(10, 2, 3),
  name = "Weight",
  type = "bar"
)

p <- subplot(p1, p2, nrows = 2, shareX = TRUE)

p %>% layout(yaxis = list(title = "Size"))

enter image description here

我想控制每个子图的y轴标题。看来我的操作方式只能控制顶部子图。如何为底部子图的y轴添加标题?

1 个答案:

答案 0 :(得分:1)

titleY = TRUE将有所作为,例如:

p1 <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "Size",
  type = "bar")%>%layout(yaxis = list(title = c("Size1")))

p2 <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(10, 2, 3),
  name = "Weight",
  type = "bar"
)%>%layout(yaxis = list(title = c("Size2")))


p <- subplot(p1, p2, nrows = 2, shareX = TRUE, titleY= TRUE) 
p

enter image description here