[R] Rotate a plot, and subplot

Paul Murrell p.murrell at auckland.ac.nz
Mon Oct 13 22:19:16 CEST 2003


Hi


Pascal A. Niklaus wrote:
> Hi all,
> 
> Is there a way to rotate a plot, e.g. a histogram, by a certain angle 
> (90/180/270 degress)? I spent hours trying to figure out how this is 
> done, but without success.


Some plots provide an argument (e.g., the horiz argument in barplot), 
but there is no general rotation transformation available for all base 
plots.

Viewports in the grid package provide arbitrary rotations, and these can 
be used with lattice plots.  This may take a bit of digesting, but since 
you're prepared to spend hours on it :) ... here's a relatively simple 
example:

myhist <- histogram(rnorm(50))
# Make two square regions side by side
push.viewport(viewport(layout=grid.layout(1, 2, respect=TRUE)))
# Go to the left region
push.viewport(viewport(layout.pos.col=1))
# Draw the histogram in normal orientation
print(myhist, newpage=FALSE)
pop.viewport()
# Go to the right region then rotate 90 degrees
push.viewport(viewport(layout.pos.col=2), viewport(angle=90))
# Draw the histogram (rotated 90 degrees)
print(myhist, newpage=FALSE)
pop.viewport(3)


That only works because I made the regions square (so a region is the 
same size when it is rotated 90 degrees).  Here's a slightly more 
complex example which will work for non-square regions ...

# Create a region in the left half of the page
push.viewport(viewport(x=0, width=0.5, just="left"))
# Draw the histogram in normal orientation
print(myhist, newpage=FALSE)
pop.viewport()
# Create a region in the right half of the page
# which is rotated 90 degrees
push.viewport(viewport(x=0.75,
                        # Make the rotated width the same as
                        # the height of the page
                        width=grid.convert(unit(1, "npc"), "npc",
                          "y", "dimension", "x", "dimension"),
                        # Make the rotated height half the
                        # width of the page
                        height=grid.convert(unit(0.5, "npc"), "npc",
                          "x", "dimension", "y", "dimension"),
                        angle=90))
# Draw the histogram (rotated 90 degrees)
print(myhist, newpage=FALSE)
pop.viewport()


> Also, I'm looking for an equivalent to the S-Plus "subplot" command to 
> insert a kind of "thumbnail" graphic into a bigger one. How is this best 
> done in R?


You can use par(fig) and/or par(plt) and par(new) to do some things, but 
these are not terribly helpful for positioning subplots within another plot.

Again, grid viewports can be used to create subregions with a lot of 
flexibility.  There is a package on CRAN called gridBase which allows 
you to create a region using a grid viewport, then draw a standard plot 
in that region (with a few restrictions).  There is a vignette with the 
package that tries to explain how it works -- the last example in there 
embeds some piecharts as subplots within a scatterplot.

Hope that helps.  Feel free to contact me directly if you have further 
questions;  I'd be interested to hear more about what you are trying to 
achieve.

Paul
-- 
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/




More information about the R-help mailing list