1

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?

user2955884
  • 405
  • 2
  • 11

1 Answers1

1

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

Quinten
  • 35,235
  • 5
  • 20
  • 53