Re: [R] levelplot and unequal cell sizes

From: hadley wickham <h.wickham_at_gmail.com>
Date: Sun, 06 May 2007 14:16:54 +0200

On 4/26/07, Deepayan Sarkar <deepayan.sarkar_at_gmail.com> wrote:
> On 4/25/07, Waichler, Scott R <Scott.Waichler@pnl.gov> wrote:
> > Hadley and Deepayan,
> >
> > Thank you for responding. Here is a simple example of what I'm talking
> > about. It is a grid that is 5 cells wide by 2 cells tall. The width of
> > the cells in the x-direction is variable; the cells at either end have
> > width = 4 units, and the three cells in the middle have width = 2 units.
> > My objective is to have the color contour boundaries fall on the cell
> > boundaries instead of equidistant between cell nodes. In the plot, I
> > want the cyan/blue and orange/gray boundaries to be located at the red
> > cell boundary lines. Also, the colored regions should extend to the
> > ends of the domain (x = 0, 14).
> >
> >
> > library(lattice)
> >
> > x.node <- rep(c(2, 5, 7, 9, 12), 2)
> > y.node <- c(rep(0.5, 5), rep(1.5, 5))
> > z <- rep(1:5, 2)
> > contour.levels <- seq(0.5, 5.5, by=1)
> > x.cell.boundary <- c(0, 4, 6, 8, 10, 14)
> > contour.colors <- c("cyan", "blue", "green", "orange", "gray")
> >
> > print(
> > levelplot(z ~ x.node * y.node,
> > panel = function(z,...) {
> > panel.levelplot(z,...)
> > panel.abline(v = x.cell.boundary, col="red")
> > },
> > xlim = range(x.cell.boundary),
> > at=contour.levels,
> > colorkey = list(space="top", width=1, height=0.9,
> > at=1:5,
> > col=contour.colors,
> > labels=list(labels=z, at=z)
> > ),
> > col.regions=contour.colors,
> > region = T,
> > contour = F
> > )
> > )

>

> You are right, panel.levelplot is indeed assuming that the boundaries
> are between consecutive midpoints. There is no built in way around
> that; there simply isn't enough information available to the panel
> function.
>

> The cleanest solution, in principle, is to write your own panel
> function that ends up calling panel.polygon or grid.polygon.
> panel.levelplot is a good starting point (the only tricky part is
> getting the colors right, almost everything else you can get rid of).
> Maybe Hadley will have a simpler solution.

Sorry, it's taken me so long to get back to you on this, but I've been travelling and internet access has been spotty.

You can do something very similar with ggtile, which already has the width and height arguments build in (but defaults to similar behaviour to lattice). If you don't mind, I'll include this example in the next version of ggplot so others can see how to use it.

Hadley



R-help_at_stat.math.ethz.ch 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. Received on Sun 06 May 2007 - 12:27:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 06 May 2007 - 16:31:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.