Multiple ggplot2 plots with plotly

Fisseha Berhane picture Fisseha Berhane · Oct 5, 2015 · Viewed 7.8k times · Source

I want to use ggplot2 with grid.arrange to generate multiple plots with plotly. Some thing similar to this:

library(ggplot2)
library(gridExtra)
library(plotly)


g1<-ggplot(mpg, aes(displ, hwy, color=factor(year)))+geom_point()

g2<-ggplot(mpg, aes(cyl, hwy, color=factor(year)))+geom_point()

g<-grid.arrange(g1,g2,ncol=2)

ggplotly(g)

However, I am getting "Error in gg2list(p) : No layers in plot"

Any suggestions

Answer

B-leR picture B-leR · Apr 30, 2017

Use the subplot function of plotly:

subplot(p1, p2, nrows = 2, margin = 0.04, heights = c(0.6, 0.4))