Embedding a miniature plot within a plot
Asked Answered
S

3

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!

Southwards answered 17/10, 2011 at 12:46 Comment(2)
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.Arbitrary
may this casoilresource.lawr.ucdavis.edu/drupal/node/1007 helps?Celestinecelestite
B
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

Blowtube answered 17/10, 2011 at 14:55 Comment(1)
Very elegant! Exactly what I hoped to find.Southwards
W
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

Witted answered 17/10, 2011 at 15:14 Comment(6)
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.Knox
Thanks for the tip, even though it is not as general as Al R.'s answer it is definatelly easier to use.Southwards
Wanna see if you can tackle these histogram insets? :) I think it'll be interesting when they arrive.Ratiocination
Only required(grid) is missing in the example I think.Danie
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.Haeckel
@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.Witted
B
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

Blowtube answered 17/10, 2011 at 14:55 Comment(1)
Very elegant! Exactly what I hoped to find.Southwards
F
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.

Fragonard answered 17/10, 2011 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.