[R] paneling spplot and hist

Jannes Münchow malnamalja at gmx.de
Fri Dec 18 14:42:38 CET 2015

Hi Pai,

there are at least two solutions two your problem.

The first solution makes use of R's base graphics instead of using the 
lattice-based spplot function:


# example more or less copied from the Applied Spatial Data Analysis 
with R book

# load the meuse dataset
# use the xy-columns to transform the data.frame into a
# SpatialPointsDataFrame (SPDF)
coordinates(meuse) <-~ x + y
# specify color palette we are going to use
pal <- RColorBrewer::brewer.pal(5, name = "Blues")
# find fisher class intervals using the fisher-algorithm
q5 <- classIntervals(meuse$zinc, n = 5, style = "fisher")
# find the colors for each point
q5_col <- findColours(q5, pal)

# set up a plot window with one row and two columns (2 panels)
par(mfrow = c(1, 2))
# plot the SPDF into the first panel
plot(meuse, pch = 16, col = q5_col)
# add a legend
legend("topleft", fill = attr(q5Colours, "palette"),
        legend = names(attr(q5Colours, "table")), bty = "n", cex = 0.5)
# plot a histogram into the second panel
hist(meuse at data$copper, main = "Histogram", xlab = "zinc")

The second solution uses the packages lattice, grid and sp.

# using sp, lattice and grid
# set up a plot window with one row and two columns (2 panels)
pushViewport(viewport(layout = grid.layout(1, 2)))
vp1 <- viewport(x = 0, y = 0,
                 height = 1, width = 0.5,
                 just = c("left", "bottom"),
                 name = "left",
                 layout = grid.layout(nrow = 1, ncol = 2))
# plot the SPDF into the first panel
p <- spplot(meuse, "zinc")
print(p, newpage = FALSE)
vp2 <- viewport(x = 1, y = 0,
                 height = 1, width = 0.5,
                 just = c("right", "bottom"),
                 name = "right")
# plot a lattice histogram into the second panel
print(histogram(meuse at data$copper, main = ""), newpage = FALSE)

For more information on the grid package and how to use it with spplot, 
you might want to read the package documentation and this blog 

Hope this helps,


Dec 14, 2015; 8:20pm Debasish Pai Mazumder 
Debasish Pai Mazumder 
paneling spplot and hist

Hi all,

I am new in R. I am trying to panel spplot and hist.

spplot(hspdf, "CDP", col = "white", col.regions = blue2red(20), sp.layout =
list(l2), at = seq(1,10,1), colorkey = list(space = "bottom", labels =
list(labels = paste(seq(1,10,1)), cex = 1.5)), sub = list("CDP", cex = 1.5,
font = 2))

hist(cdp.obsc, col="grey", border="grey", main="CDP", probability=T)
lines(c.breaks, obs.cdp.d, col="blue")
lines(c.breaks, obs.cdp.e, col="red")

I have tried with par(mfrow=c(2,1))and layout(matrix...., both don't work.

Any advice?

with regards

         [[alternative HTML version deleted]]

[hidden email] 
mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list