[R-sig-Geo] plotting unequal contiguous spatial grids

Mikkel Grum mi2kelgrum at yahoo.com
Sun Mar 26 20:57:43 CEST 2006


# 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




More information about the R-sig-Geo mailing list