Home > other >  Axis labels lost in plotly::subplot()
Axis labels lost in plotly::subplot()

Time:01-17

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?

CodePudding user response:

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

  • Related