How to draw multiple plots in a single figure?

44 views
Skip to first unread message

Sang Chul Choi

unread,
Aug 27, 2009, 12:01:35 PM8/27/09
to ggplot2
Hi,

I used to use
> par(mfrow=c(2,2))

to plot multiple plots in a single figure. I have tried to use it with
gplot. It just draws a single figure. Should I use some other tricks
in gplot e.g., facet_grid? Can't I use mfrow any more?

I will appreciate any help.

Thank you,

Sang Chul

Luciano Selzer

unread,
Aug 27, 2009, 2:37:18 PM8/27/09
to Sang Chul Choi, ggplot2
Hi Sang Chul,
 
you must use viewports with ggplot2 in order to obtain multiple graphics in a single figure. As far as I know par options doesn't affect graphics made with ggplot2.
Here is an example code to do what you want. I hope it helps.
Bye 
 
p1 = ggplot(...)+...
p2 = ggplot(...)+...
p3 = ggplot(...)+...
p4 = ggplot(...)+...
 
vplayout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
grid.newpage()
pushViewport(viewport(layout=grid.layout(2,2)))
 
print(p1, vp=vplayout(1,1))
print(p2, vp=vplayout(1,2))
print(p3, vp=vplayout(2,1))
print(p4, vp=vplayout(2,2))
 
2009/8/27 Sang Chul Choi <gos...@gmail.com>
>
>
 
 
 
--
Luciano
 

baptiste auguie

unread,
Aug 27, 2009, 3:27:13 PM8/27/09
to ggplot2
Hi,

Same idea using the gridextra package,

source("http://gridextra.googlecode.com/svn/trunk/R/arrange.r")
arrange(p1, p2, p3, p4)


HTH,

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

Osmo Salomaa

unread,
Aug 28, 2009, 8:12:32 AM8/28/09
to ggplot2
On Aug 27, 9:37 pm, Luciano Selzer <luciano.sel...@gmail.com> wrote:
> you must use viewports with ggplot2 in order to obtain multiple graphics in
> a single figure.

I have a question related to this: when using viewports, is there a
better way to line up the y-axes of two plots drawn on top of each
other than by padding the y-axis labels with zeros or Unicode figure
spaces to the same width with scale_y_continuous(formatter=...)?

hadley wickham

unread,
Aug 28, 2009, 8:21:44 AM8/28/09
to Osmo Salomaa, ggplot2
> I have a question related to this: when using viewports, is there a
> better way to line up the y-axes of two plots drawn on top of each
> other than by padding the y-axis labels with zeros or Unicode figure
> spaces to the same width with scale_y_continuous(formatter=...)?

Not yet, but there will be in the future - it will be possible to say,
allocate 2 cm for the y axis label, regardless of how much space it
actually needs.

Hadley


--
http://had.co.nz/

Mike Lawrence

unread,
Aug 29, 2009, 6:11:22 AM8/29/09
to hadley wickham, ggplot2
Will something similar apply to the x-axis and legend? It would be
nice to guarantee that the plot area itself is identical between two
graphs regardless of the axis or legend labels.
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

hadley wickham

unread,
Aug 29, 2009, 9:35:51 AM8/29/09
to Mike Lawrence, ggplot2
Yes, definitely! You'll be able to control the size of every plot element.
Hadley
--
http://had.co.nz/

Eve

unread,
Sep 8, 2009, 2:04:49 PM9/8/09
to ggplot2
This is very cheeky Hadley, but is there a rough time-line available
for the launch of this increased flexibility in controlling plot
parameters for complex plot layouts? Thanks for a really useful couple
of packages by the way.
Eve

On Aug 29, 2:35 pm, hadley wickham <h.wick...@gmail.com> wrote:
> Yes, definitely!  You'll be able to control the size of every plot element.
> Hadley
>
>
>
>
>
> On Sat, Aug 29, 2009 at 5:11 AM, Mike Lawrence<Mike.Lawre...@dal.ca> wrote:
> > Will something similar apply to the x-axis and legend? It would be
> > nice to guarantee that the plot area itself is identical between two
> > graphs regardless of the axis or legend labels.
>

hadley wickham

unread,
Sep 12, 2009, 11:30:37 AM9/12/09
to Eve, ggplot2
> This is very cheeky Hadley, but is there a rough time-line available
> for the launch of this increased flexibility in controlling plot
> parameters for complex plot layouts? Thanks for a really useful couple
> of packages by the way.

Unfortunately not - I really need a week or so of time to devote to
the problem. Maybe over Christmas.

Hadley


--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages