22

Does anybody know of a general way to embed plots into other plots to produce something like the mockup below?

I know that in lattice you can do it with print(..., more=TRUE, positions=...) as explained in this question, and I guess ggplot has a solution to it aswell (but I'm not very good with ggplot). The problem is that I want to embed a regular plot from the survival package that use the standard graphics package into a lattice plot.

A mockup of an embedded plot

Thanks in advance!

Community
  • 1
  • 1
Backlin
  • 14,612
  • 2
  • 49
  • 81
  • I think I would give up and do it in post-process.. depends on the target of course. If it's PPT presentation, then it's very easy. – Tomas Oct 17 '11 at 13:42
  • 1
    may this http://casoilresource.lawr.ucdavis.edu/drupal/node/1007 helps? – EDi Oct 17 '11 at 13:52

3 Answers3

26

And here is a way to do it the other way around, ggplot2 graphic in a base graphic:

require(ggplot2)
require(grid)

plot(sin, -pi, 2*pi)
qp <- qplot(mpg, wt, data=mtcars)
print(qp, vp=viewport(.8, .75, .2, .2))

enter image description here

ROLO
  • 4,183
  • 25
  • 41
  • That looks like it would be very general (as well as incredibly concise), since lattice plots (also using the grid-engine) could also be embedded and you seem to be able do any-grid on any-grid combination that I have tried. – IRTFM Oct 17 '11 at 15:26
  • Thanks for the tip, even though it is not as general as Al R.'s answer it is definatelly easier to use. – Backlin Oct 18 '11 at 14:02
  • Wanna see if you can tackle [these histogram insets](http://blog.revolutionanalytics.com/2011/10/ggplot2-for-big-data.html)? :) I think it'll be interesting when they arrive. – Iterator Oct 28 '11 at 01:44
  • Only `required(grid)` is missing in the example I think. – Juan Apr 27 '13 at 01:52
  • And then, if you use ggsave to save it as pdf, the only plot that appears on the result file will be the last plot. – Eduardo Jun 04 '13 at 11:46
  • @Eduardo: that is the intended behaviour of ggsave. To save a pdf use pdf(file='somefile.pdf') to start and dev.off() to stop writing to file. – ROLO Jun 04 '13 at 13:37
18

You could try the gridBase package which provides some functionality for integrating base and grid-based graphics (including lattice and ggplot2). The example below embeds a base graphics plot inside of a lattice plot.

library(lattice)
library(gridBase)
library(grid) 

plot.new()
pushViewport(viewport())
xvars <- rnorm(25)
yvars <- rnorm(25)
xyplot(yvars~xvars)
pushViewport(viewport(x=.6,y=.8,width=.25,height=.25,just=c("left","top")))
grid.rect()
par(plt = gridPLT(), new=TRUE)
plot(xvars,yvars)
popViewport(2)

More detail here: http://casoilresource.lawr.ucdavis.edu/drupal/node/1007 And here: http://cran.r-project.org/web/packages/gridBase/vignettes/gridBase.pdf

Dieter Menne
  • 10,076
  • 44
  • 67
Al R.
  • 2,430
  • 4
  • 28
  • 40
3

Check out the Teaching Demos package package TeachingDemos package - and the subplot() function It might work on the lattice as well - haven't tried it though.

Rainer
  • 8,347
  • 1
  • 23
  • 28