Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 977
user2955884 Avatar asked Dec 05 '25 01:12

user2955884


1 Answers

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

like image 165
Quinten Avatar answered Dec 06 '25 17:12

Quinten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!