Liky
Liky

Reputation: 1247

Add title to each y-axis with Plotly subplots

Let's consider the following plot:

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

I would like to control the y-axis title of each subplot. It looks like the way I am doing it I only manage to control the top subplot. How can I also add a title for the y-axis of the bottom subplot?

Upvotes: 1

Views: 1355

Answers (1)

Carles
Carles

Reputation: 2829

titleY = TRUE will make the difference, such that:

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

Upvotes: 6

Related Questions