Skip to content

plotting unequal contiguous spatial grids

3 messages · Edzer Pebesma, Mikkel Grum

#
# Is there a way to plot contiguous spatial grids on
# the same plot, even when the grids have 
# unequal spacing? An example would be:

df1 <- data.frame(expand.grid(x = 1:3, y = 1:4), z =
rnorm(12, 1, 0.01))
df2 <- data.frame(expand.grid(x = 1:3, y = 5:7), z =
rnorm(9, 2, 0.01))
df3 <- data.frame(expand.grid(x = 4:6, y = seq(1, 7,
1.5)), z = rnorm(15, 3, 0.01))

# If I just wanted points, I would do the following:
plot(df1[, 1:2], xlim = c(0, 7), ylim = c(0, 8), type
= "n")
points(df1, col = "blue")
points(df2, col = "red")
points(df3, col = "grey")

# If the grids had equal spacing, the following would
# work (in this case it doesn't):
library(sp)
df <- rbind(df1, df2, df3)
coordinates(df) <- ~x+y
gridded(df) <- TRUE
spplot(df)

# I had hoped that I could do something like: 
coordinates(df1) <- ~x+y
gridded(df1) <- TRUE
coordinates(df2) <- ~x+y
gridded(df2) <- TRUE
coordinates(df3) <- ~x+y
gridded(df3) <- TRUE

spplot(df1, xlim = c(0, 7), ylim = c(0, 8))
spplot(df2, add = TRUE)
spplot(df3, add = TRUE)
# but that doesn't work, so I tried:

spplot(xlim = c(0, 6), ylim = c(0, 7),
    panel = function(x, y, subscripts, . . .) {
        panel.spplot(df1),
        panel.spplot(df2),
        panel.spplot(df3)
    }
)
# which doesn't work either. 
# Is there any way round this?

Regards,
Mikkel Grum

Genetic Diversity
International Plant Genetic Resources Institute
#
Mikkel, I've looked at it for a while but can't see an easy
solution to this, apart from using

image(df1, xlim = c(0, 6), ylim = c(0, 7))
image(df2, add = T)
image(df3, add = T)

spplot basically wraps levelplot for grids, and I don't see an
easy way out using that for multiple grids with different cell
sizes; see what happens if you join the grid cell centres as
points and feed them to levelplot as a data.frame, probably
the grid cell sizes levelplot will choose will not suit you.

A solution might be to try to do it on the level of package
grid, but my skills are not sufficient for that. Another would
be to consult Deepayan (through R-help).
--
Edzer
Mikkel Grum wrote:
1 day later
#
Edzer, Thanks for your help. For some reason I had it
that you couldn't use add = TRUE with image(). I
thought I had tested it. It will work for me.

Using the grid centres to create a new grid with
levelplot would in some cases give me problems with
grid sizes (many more grids cells).

I'll have a look at whether I need to reformulate my
query and send it to R-help, or whether the image
function will be adequate for all that I need to do.

Mikkel
--- "Edzer J. Pebesma" <e.pebesma at geo.uu.nl> wrote: