Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly does not show titles of multiple plots in R

Tags:

r

ggplot2

plotly

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.

like image 938
umair durrani Avatar asked Dec 19 '25 14:12

umair durrani


1 Answers

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

like image 84
bcd Avatar answered Dec 22 '25 07:12

bcd



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!