user2955884
user2955884

Reputation: 539

Axis labels lost in plotly::subplot()

Continuing the example introduced in grid.arrange with ggplotly

library(ggplot2)
library(gridExtra)
library(plotly)

d <- data.frame(x=1:20,y=1:20, z=20:1)
p1 <- ggplot(data=d) +
  geom_point(aes(x=x, y=y)) +
  xlab("X") + ylab("Y")
p2 <- ggplot(data=d) +
  geom_point(aes(x=x, y=z)) +
  xlab("X") + ylab("Z")
ggplotly(p1)
ggplotly(p2)

Axis labels are lost with subplot:

ggplotly(p1)
ggplotly(p2)
ply1 <- ggplotly(p1)
ply2 <- ggplotly(p2)
subplot(ply1, ply2, nrows=1)

How could I actually keep axis labels?

Upvotes: 3

Views: 238

Answers (1)

Quinten
Quinten

Reputation: 41225

You could use titleY and titleX in your subplot to add the labels. With margin you could add some whitespace between the plots like this:

library(ggplot2)
library(gridExtra)
library(plotly)

d <- data.frame(x=1:20,y=1:20, z=20:1)
p1 <- ggplot(data=d) +
  geom_point(aes(x=x, y=y)) +
  xlab("X") + ylab("Y")
p2 <- ggplot(data=d) +
  geom_point(aes(x=x, y=z)) +
  xlab("X") + ylab("Z")

ply1 <- ggplotly(p1)
ply2 <- ggplotly(p2)
subplot(ply1, ply2, nrows=1, 
        titleY = TRUE, 
        titleX = TRUE,
        margin = 0.05)

Created on 2023-01-16 with reprex v2.0.2

Upvotes: 3

Related Questions