Bernhard
2008-Jun-11 05:40 UTC
[R] Problem when combining dotplot() and textplot() using grid
Hi everyone. I want to solve the following problem. I have a data.frame and I create a dotplot using lattice. Then I want to use the grid-package to create a combined graphic which contains the dotplot as well as a textplot() (using package gplots) of the data.frame next to the dotplot. Example code: library(lattice) library(grid) library(gplots) xx <- data.frame(f=factor(rep(1:5, each=5)), gr= rep(c("gr1", "gr2", "gr3", "gr4", "gr5"), 5), val=rnorm(25)) grid.newpage() pushViewport(viewport(layout = grid.layout(1, 2))) pushViewport(viewport(layout.pos.col=1, layout.pos.row=1)) p <- dotplot(f ~ val, groups=gr, xx, cex=1.7, pch=20) print(p, newpage=FALSE) popViewport(1) pushViewport(viewport(layout.pos.col=2, layout.pos.row=1)) textplot(xx, show.rownames=FALSE) popViewport(1) Obviously, this does not work since textplot() seems to call plot.new no matter what options are set with par(). My question: Is it possible to archieve my goal to plot the data.frame with textplot() next to the dotplot? And furthermore, if it is possible to do so, is it even possible to adjust the textplot in a way that the each row of the data.frame when plotted using textplot() is at the same height than the corresponding row in the dotplot? I guess that this is quite tricky and any hint on which package or functions I could use would be very helpful. Thank you very much. Bernhard
Paul Murrell
2008-Jun-11 22:14 UTC
[R] Problem when combining dotplot() and textplot() using grid
Hi Bernhard wrote:> Hi everyone. > > I want to solve the following problem. I have a data.frame and I > create a dotplot using lattice. > > Then I want to use the grid-package to create a combined graphic which > contains the dotplot as well as a textplot() (using package gplots) of > the data.frame next to the dotplot. > > Example code: > library(lattice) > library(grid) > library(gplots) > > xx <- data.frame(f=factor(rep(1:5, each=5)), gr= rep(c("gr1", "gr2", > "gr3", "gr4", "gr5"), 5), val=rnorm(25)) > grid.newpage() > pushViewport(viewport(layout = grid.layout(1, 2))) > pushViewport(viewport(layout.pos.col=1, layout.pos.row=1)) > p <- dotplot(f ~ val, groups=gr, xx, cex=1.7, pch=20) > print(p, newpage=FALSE) > popViewport(1) > > pushViewport(viewport(layout.pos.col=2, layout.pos.row=1)) > textplot(xx, show.rownames=FALSE) > popViewport(1) > > Obviously, this does not work since textplot() seems to call plot.new > no matter what options are set with par(). > > My question: Is it possible to archieve my goal to plot the data.frame > with textplot() next to the dotplot?This should work (adjustments to your code are accompanied by comments): library(lattice) library(grid) library(gplots) # Also need gridBase library(gridBase) # Start a traditional graphics page plot.new() xx <- data.frame(f=factor(rep(1:5, each=5)), gr= rep(c("gr1", "gr2", "gr3", "gr4", "gr5"), 5), val=rnorm(25)) grid.newpage() pushViewport(viewport(layout = grid.layout(1, 2))) pushViewport(viewport(layout.pos.col=1, layout.pos.row=1)) p <- dotplot(f ~ val, groups=gr, xx, cex=1.7, pch=20) print(p, newpage=FALSE) popViewport(1) pushViewport(viewport(layout.pos.col=2, layout.pos.row=1)) # Prevent textplot() starting new page par(new=TRUE) # Locate traditional plot region based on grid viewport par(plt=gridPLT()) textplot(xx, show.rownames=FALSE) popViewport(1)> And furthermore, if it is possible to do so, is it even possible to > adjust the textplot in a way that the each row of the data.frame when > plotted using textplot() is at the same height than the corresponding > row in the dotplot? I guess that this is quite tricky and any hint on > which package or functions I could use would be very helpful.This is starting to push things a bit; you are getting to the point where it might be easier to start drawing things yourself. However, here's one approach that might give you what you want. The idea is to draw the textplot() for each level of the factor in its own viewport (to get the alignment with the scale on the lattice plot). # Start a traditional graphics page plot.new() grid.newpage() # Viewport in left half of page to leave space for text pushViewport(viewport(x=0, width=.5, just="left")) p <- dotplot(f ~ val, groups=gr, xx, cex=1.7, pch=20) print(p, newpage=FALSE, # Control name of lattice viewport so we can get back to it prefix="lattice") # Leave all viewports so we can navigate back upViewport(1) # Go back into the lattice plot viewport to add text bits downViewport("lattice.panel.1.1.off.vp") # For each level of 'f' for (i in levels(xx$f)) { # Push a viewport off to the side pushViewport(viewport(x=unit(1, "npc") + unit(1, "cm"), # Here's the vertical alignment bit y=unit(as.numeric(as.character(i)), "native") + unit(1, "lines"), height=1/(length(levels(xx$f)) + 1), just=c("left", "top"))) # Just to show where we are (can be removed) grid.rect(gp=gpar(col="grey")) # Align the traditional plot region par(new=TRUE) par(fig=gridFIG()) # Fiddle with size of text par(cex=0.7) # Draw the relevant subset of the data frame textplot(xx[xx$f == i, ], halign="left", valign="top", mar=rep(0, 4)) popViewport() } # Final tidy up upViewport(0) Bits of this are very your-example-specific, but this might be enough for a one-off. Paul> Thank you very much. > Bernhard > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/