Search code examples
rgraphicscustomizationr-grid

Make resizable plots using the grid graphing system in R


Recently I read about the grid graphing system in R. It is very flexible and with its mastery one should be able to make very sophisticated graphs. However I have not found any good place that will allow me to plot a graph that is also re-sizable? The question is as follows: How do you use grid graphing system in R so that the final output is actually resizable?


Solution

  • One way of doing so is not using the grip graphing system directly, but use the lattice interface to it. The lattice package comes installed with R as far as I know, and forms a very flexible interface to the underlying Trellis graphs, which are grid-based graphs. Lattice also allows you to manipulate the grid directly, so in fact for most sophisticated graphs that will be all you need.

    If you really are going to work with the grid graphing system itself, you have to use the correct coordinate system for it to be scalable. Either "native", "npc" (Normalized Parent Coordinates) or "snpc" (Square Normalized Parent Coordinates) allow you to rescale a figure, as they give the coordinates relative to the size (or one aspect of it) of the current viewport.

    In order to make full use of these, make sure you understand the concept of viewports very well. I have to admit that I still have a lot to learn about it. If you really want to get on with it, I can suggest the book R Graphics from Paul Murrell

    Take a closer look at chapter 5 of that book. You can also learn a lot from the R code of the examples, which can also be found on this page

    To give you one :

    grid.circle(x=seq(0.1, 0.9, length=100), 
                y=0.5 + 0.4*sin(seq(0, 2*pi, length=100)),
                r=abs(0.1*cos(seq(0, 2*pi, length=100))))
    

    Perfectly scaleable. If you look at the help pages of grid.circle, you'll find the default.units="npc" option. That's where in this case the correct coordinate system is set. Compare to

    grid.circle(x=seq(0.1, 0.9, length=100), 
                y=0.5 + 0.4*sin(seq(0, 2*pi, length=100)),
                r=abs(0.1*cos(seq(0, 2*pi, length=100))),
                default.units="inch")
    

    which is not scaleable.