Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ggplot2 plots with plotly

Tags:

r

ggplot2

plotly

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

like image 278
Fisseha Berhane Avatar asked Oct 05 '15 18:10

Fisseha Berhane


1 Answers

Use the subplot function of plotly:

subplot(p1, p2, nrows = 2, margin = 0.04, heights = c(0.6, 0.4))
like image 113
B-leR Avatar answered Sep 29 '22 02:09

B-leR