简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM