[R] Put two plots side by side

Felipe Carrillo mazatlanmexico at yahoo.com
Mon May 31 20:01:47 CEST 2010


Two different ways:
library(ggplot2)
x=5
size=50
A=data.frame(X=sample(x, size, replace=T), Y=sample(x, size, replace=T),a=rep(1:2,each=25));A
# Facetting
qplot(X,Y,data=A) + geom_jitter(position=position_jitter(width=.03)) + facet_grid(.~a)
# Or with vp
p=qplot(X, Y, data=A) + geom_jitter(position=position_jitter(width=.03))
ggsave(p, file='main.png')
p1=qplot(X, Y, data=A) + geom_jitter(position=position_jitter(width=.3))
ggsave(p1, file='main2.png')
vport <- function(x, y)
viewport(layout.pos.row=x, layout.pos.col=y)
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
print(p, vp=vport(1,1))
print(p1, vp=vport(1,2))
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA



----- Original Message ----
> From: Erik Iverson <eriki at ccbr.umn.edu>
> To: Peng Yu <pengyu.ut at gmail.com>
> Cc: ggplot2 <ggplot2 at googlegroups.com>
> Sent: Sun, May 30, 2010 7:45:22 PM
> Subject: Re: Put two plots side by side
> 
> 
> 
> I want to put the above two plots side by side (essentially, 
> to mimic
> par(mfrow=c(1,2)) in the traditional graphic system). Is there 
> a
> convenient way to do so in ggplot2?
> 

Yes.

My 
> understanding is that you need to create use grid functions to do this. You can 
> create a viewport with a layout (using grid.layout) and then print the ggplot2 
> objects using the vp argument.  I don't know if there is a ggplot2 
> abstraction for this idea.

-- You received this message because you are 
> subscribed to the ggplot2 mailing list.
Please provide a reproducible 
> example: http://gist.github.com/270442

To post: email > ymailto="mailto:ggplot2 at googlegroups.com" 
> href="mailto:ggplot2 at googlegroups.com">ggplot2 at googlegroups.com
To 
> unsubscribe: email ggplot2+> href="mailto:unsubscribe at googlegroups.com">unsubscribe at googlegroups.com
More 
> options: http://groups.google.com/group/ggplot2






More information about the R-help mailing list