plotly 不显示 R 中多个图的标题

plotly does not show titles of multiple plots in R

我正在尝试使用 ggplot2plotly 将 2 个地块布置在一起。这是我尝试过的:

library(ggplot2)
library(plotly)

mt_mpg <- ggplot(data = mtcars)+
  geom_boxplot(aes(x = as.factor(cyl), y = mpg))+
  ggtitle("mpg vs cyl")

mt_disp <- ggplot(data = mtcars)+
  geom_boxplot(aes(x = as.factor(cyl), y = disp))+
  ggtitle("disp vs cyl")

subplot(mt_mpg, mt_disp)  

一切正常,但组合图的标题仅包含 "disp vs cyl"。我想在相应图的顶部包含这两个标题。但是我在 subplot() 命令中看不到任何选项。任何想法如何解决这个问题?谢谢

一种方法是使用 facet_wrap 而不是 ggtitle。例如:

df <- mtcars
df$lab1 <- 'mpg vs cyl'
df$lab2 <- 'disp vs cyl'

mt_mpg <- ggplot(df)+
  geom_boxplot(aes(x = as.factor(cyl), y = mpg))+
  facet_wrap(~lab1)

mt_disp <- ggplot(df)+
  geom_boxplot(aes(x = as.factor(cyl), y = disp))+
  facet_wrap(~lab2)

subplot(mt_mpg, mt_disp)

干杯,

布兰登