Is there any way to produce multiple plot panels other than with facet?

58 views
Skip to first unread message

Oren

unread,
Mar 9, 2009, 1:27:49 PM3/9/09
to ggplot2
Facet is a bit inflexible, e.g., if you want to plot multiple data
sets in subpanels of a single display. Is there a way to do this more
procedurally, addressing each panel / facet separately?

Wayne Folta

unread,
Mar 9, 2009, 1:54:16 PM3/9/09
to Oren, ggplot2
First, a facetious answer I just couldn't resists: It's called lattice.

Seriously, though, to simply plot separate data sets in subpanels of a
single display, you can join your sub data sets into one data frame,
with a new dummy variable to indicate which data set each entry is
from. Then use this dummy variable to facet. Or do I misunderstand the
task?

Wayne

Oren Cheyette

unread,
Mar 9, 2009, 2:07:02 PM3/9/09
to Wayne Folta, ggplot2
Hi Wayne - Thanks for the reply.

The problem I ran into with "lattice" was the learning curve, plus some hints in the documentation that it wasn't flexible enough for what I was trying to do. Plus I'd already invested some time in learning ggplot and hoped to leverage that.

I tried exactly the trick you suggested, but ran into problems just getting "facet" to work at all (and I could foresee other difficulties). After setting up the plot with a series of geom() calls, I get this error:
p <- p + facet_grid(SPREAD ~ DURATION);
> p;
Error: Casting formula contains variables not found in molten data: SPREAD, DURATION

Here's a sample of the data
> zz[1:10,]

   qmin  qmax qmed       QT10       QT25       QT70        QT75        QT90 SIDE SPREAD DURATION
7     1     5    3 -1.0604916 -0.6014588 -0.2684699 -0.24039957 -0.13602747    S      1        1
8     5    25   10 -0.8327069 -0.6283954 -0.2516709 -0.21588915 -0.01946848    S      1        1
9    25   100   39 -0.8234039 -0.6363954 -0.2870261 -0.24409572 -0.04818910    S      1        1
10  100   250  100 -0.8133094 -0.5385327 -0.1848606 -0.14143743  0.03766133    S      1        1
11  250  1000  445 -0.5572732 -0.3755122 -0.0854282 -0.06559869  0.00000000    S      1        1
12 1000 10000 2000 -0.3354031 -0.1747892  0.0000000  0.00400000  0.07468848    S      1        1
19    1     5    2 -0.5918587 -0.4192685 -0.1612689 -0.13238416  0.01562547    S      1        1
20    5    25   10 -0.6626855 -0.4420250 -0.2005011 -0.16631307 -0.07865400    S      1        1
21   25   100   35 -0.5575641 -0.3918947 -0.1455307 -0.12383342 -0.04228311    S      1        1
22  100   250  120 -0.5081651 -0.3179853 -0.1085723 -0.08703290 -0.01013723    S      1        1

It sure looks like the variables are present in the data, so I'm not sure what I was doing wrong.



From: Wayne Folta <wd...@mac.com>
To: Oren <oche...@gmail.com>
Cc: ggplot2 <ggp...@googlegroups.com>
Sent: Monday, March 9, 2009 10:54:16 AM
Subject: Re: Is there any way to produce multiple plot panels other than with facet?

baptiste auguie

unread,
Mar 9, 2009, 2:09:28 PM3/9/09
to Wayne Folta, ggp...@googlegroups.com
You can use grid.layout to place several ggplots on the same page.
There is however a difficulty if you want them to have a common axis
for example. I'm not aware of any obvious trick to make sure the plots
are aligned and of the same size, for instance (latticeExtra has the
c.trellis method for this). Is this what you have in mind?

baptiste
--

_____________________________

Baptiste Auguié

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

Phone: +44 1392 264187

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

squid

unread,
Apr 29, 2009, 5:51:30 AM4/29/09
to ggplot2
Did anyone figure this out? I'd like to do exactly that...have two
plots, one on top of the other in which the X axis is shared
(preferably in between the two figures, but it would be ok if it had
to be at the bottom. In other words, create a joined plot which looks
like

PlotA
xaxis
PlotB

rather than

PlotA
PlotB
x axis

The data are quite dense and on extremely different scales, otherwise
I'd just use a second Y axis on one plot.
Thanks.


On Mar 9, 7:09 pm, baptiste auguie <bapt4...@googlemail.com> wrote:
> You can usegrid.layout to place several ggplots on the same page.

hadley wickham

unread,
Apr 29, 2009, 9:12:33 AM4/29/09
to squid, ggplot2
For your example, all you need to do is:

qplot(block, value, data = rm, colour = var, facets = . ~ type)

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

squid

unread,
Apr 29, 2009, 9:24:54 AM4/29/09
to ggplot2
Hi Hadley,
I don't see how that works when the Y axes for the two plots are on
such different scales(0.001 vs 10000)? With facets, there doesn't seem
to be a way to tweak axes independently within the separate facets.
Thanks.

hadley wickham

unread,
Apr 29, 2009, 10:07:37 AM4/29/09
to squid, ggplot2
You just need to use the fuller facet specification:

qplot(block, value, data = rm, colour = var) +
facet_grid(. ~ type, scales = "free_y")

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

Damon

unread,
Apr 29, 2009, 3:26:34 PM4/29/09
to ggplot2
What should I do if the two plots are based on completely different
geoms?

I would like to draw PlotA on top of PlotB on top of a shared time
axis, where PlotA is the queue length as a function of time, and PlotB
is related to queue utilization and drops. I have developed custom
geoms to plot these two things nicely (for example, I sample queue
size to reduce the number of plot points, then I draw a line for
sampled queue size as well as a polygon to show the minimum and
maximum; for PlotB I shade in certain areas on the plot that can be
interpreted as amount of idleness and amount of drops).

In lattice, I can call xyplot, and tell it to use a custom panel
function for each of the two plots. In ggplot2, how do I tell it to
use different geoms for the two panels?

Damon.

baptiste auguie

unread,
Apr 29, 2009, 3:44:28 PM4/29/09
to ggplot2
Try this perhaps,

dummy <- data.frame(x=seq(1, 10, length=100), y=rnorm(100),
type=factor(c("f1", "f2")))

d1 <- data.frame(x=1:10, y=sin(1:10), type="f1")
d2 <- data.frame(x=1:10, y=cos(1:10), type="f2")

ggplot(data = dummy, mapping = aes(x = x, y = y)) +
facet_grid(type~., scale="free") +
layer(data=d1, geom = c( "line"), stat = "identity" ) +
layer(data=d2, geom = c( "point"), stat = "identity" )

baptiste
Reply all
Reply to author
Forward
0 new messages