[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:

library("sp")
library("RColorBrewers")
library("classInt")


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

# load the meuse dataset
data(meuse)
# 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
library("grid")
library("lattice")
grid.newpage()
# 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))
pushViewport(vp1)
# plot the SPDF into the first panel
p <- spplot(meuse, "zinc")
print(p, newpage = FALSE)
upViewport(1)
vp2 <- viewport(x = 1, y = 0,
                 height = 1, width = 0.5,
                 just = c("right", "bottom"),
                 name = "right")
pushViewport(vp2)
# plot a lattice histogram into the second panel
print(histogram(meuse at data$copper, main = ""), newpage = FALSE)
upViewport(1)


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 
http://teachpress.environmentalinformatics-marburg.de/2013/06/plotting-multiple-plots-on-one-page-with-the-grid-package-3/

Hope this helps,

Jannes


Dec 14, 2015; 8:20pm Debasish Pai Mazumder 
<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=user_nodes&user=381655> 
Debasish Pai Mazumder 
<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=user_nodes&user=381655> 
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
-Pai

         [[alternative HTML version deleted]]

______________________________________________
[hidden email] 
<http://r.789695.n4.nabble.com/user/SendEmail.jtp?type=node&node=4715547&i=0> 
mailing list -- To UNSUBSCRIBE and more, see
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.


More information about the R-help mailing list