简体   繁体   中英

plotly does not show titles of multiple plots in R

I am trying to layout 2 plots together using ggplot2 and plotly . Here's what I tried:

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)  

Everything works great but the title of the combined plot only contains "disp vs cyl". I want to include both titles on the top of their corresponding plots. But I don't see any option in subplot() command to do so. Any ideas how this can be fixed? Thanks.

one way is to use facet_wrap instead of ggtitle . For example:

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)

Cheers,

Branden

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