4

I'm trying to create multiple graphs on one panel. I'm hoping for a box plot on both sides of a spaghetti plot.

This is an example of my code:

par(mfrow=c(1,3))

boxplot(h~y,dat,
xlab="Y",
ylab="Incidence 1 (percent)",
main="H",
scales=list(x=list(at=c(1,2))))

xyplot(H~Yr,groups=Subject,type="b",data=data,
ylab="Incidence (percent)",
xlab="Year",
main="Incidence",
scales=list(x=list(at=c(1,2))))

boxplot(h1~y1,dat1,
xlab="Y",
ylab="Incidence 2 (percent)",
main="R",
scales=list(x=list(at=c(1,2))))

When I plot my first box plot things look ok (there is still empty space ready to be filled), but once the spaghetti plot is added in, the whole graph is the spaghetti plot (the box plot is erased).

Is there a way to do multiple but different types of graphs on one panel?

mnel
  • 113,303
  • 27
  • 265
  • 254
  • I know there's an optional `layout` argument to `xyplot`, although I'm afraid I've never bothered to use it. – Señor O Nov 19 '12 at 05:33
  • Look at `gridExtra` and `grid.arrange` – mnel Nov 19 '12 at 05:34
  • 2
    In your searches of the r-help archives you should include the terms lattice and base graphics. They have different coordiante systems, so mixing them is fairly tricky. You might want to look at Paul Murrell's `gridBase` package. – IRTFM Nov 19 '12 at 05:49
  • 1
    Especially, look at [this answer](http://stackoverflow.com/a/13022137/1385941) and question. `ggplot2` and `lattice` both use `grid` graphics (so you can replace any `ggplot` references to `lattice` within reason. – mnel Nov 19 '12 at 05:59

1 Answers1

1

As @DWin and @mnel point out, you are having trouble because you are trying to mix base graphics (boxplot()) and grid-based graphics (xyplot()). To get two boxplots and a spaghetti plot in a single figure, you have three main options. The first two will be much easier than the third:

  1. Use just base graphics (here boxplot() and plot( , type="b")), arranging them in a single figure with par(mfrow=c(1,3).
  2. Use just grid-based graphics (here the lattice functions bwplot() and xyplot( , type="b")), arranging them in a single figure with grid.arrange() from the gridExtra package.
  3. Use a mixture of base and grid-based graphics (like you're trying to do now), combining them in a single figure with functions from the gridBase package.

The only thing to be said for option 3 is that pursuing it will teach you a lot about the low-level implementation of both the base and grid graphical systems!

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455