[R-sig-Geo] SpatialPolygon with the max value gets no color assigned in spplot function when using "at" parameter

Roger Bivand Roger.Bivand at nhh.no
Wed Jan 30 20:04:26 CET 2013


On Wed, 30 Jan 2013, Jan Hornych wrote:

> Hi,
> I would like to do coloring of map regions based on the region values
> "weight". The approach I am taking is first to break regions into equal
> intervals,
> classIntervals(spdf$weight,4)$brks #4 intervals in this case
> and than coloring all regions within the interval with the same color
> col = brewer.pal(4,"RdYlGn"))
> The max "weight" is as well the boundary of the interval The problem is
> that the last region is not included into the last interval nad has thus no
> color. I do belive I am missing something, because any other solution to
> manually manipulate the intervals, is overcomplicating the simplicity of
> classIntervals function.
> any idea how to get all rectangles colored?
> Thank you
> Jan
> here is the example, see the rectangle in the right upper corner, it
> remains white.
> library(sp)
> library(classInt)
> library(RColorBrewer)
> p1=cbind(c(0,0,1,1,0),c(0,1,1,0,0));colnames(p1)=c("x","y")
> p2=cbind(c(0,0,1,1,0),c(1,2,2,1,1));colnames(p2)=c("x","y")
> p3=cbind(c(1,1,2,2,1),c(0,1,1,0,0));colnames(p3)=c("x","y")
> p4=cbind(c(1,1,2,2,1),c(1,2,2,1,1));colnames(p4)=c("x","y")
> p5=cbind(c(2,2,3,3,2),c(0,1,1,0,0));colnames(p5)=c("x","y")
> p6=cbind(c(2,2,3,3,2),c(1,2,2,1,1));colnames(p6)=c("x","y")
> ps1=Polygons(list(Polygon(p1)), ID="ps1")
> ps2=Polygons(list(Polygon(p2)), ID="ps2")
> ps3=Polygons(list(Polygon(p3)), ID="ps3")
> ps4=Polygons(list(Polygon(p4)), ID="ps4")
> ps5=Polygons(list(Polygon(p5)), ID="ps5")
> ps6=Polygons(list(Polygon(p6)), ID="ps6")
> sps = SpatialPolygons(list(ps1,ps2,ps3, ps4, ps5, ps6))
> spdf=
> SpatialPolygonsDataFrame(sps,data.frame(weight=c(0.1,0.1,0.3,0.4,0.45,0.6),row.names=c("ps1","ps2","ps3","ps4",
> "ps5","ps6")))
> sp.theme(set = TRUE, regions = list(col = brewer.pal(4,"RdYlGn")))
> spplot(spdf, "weight", at=classIntervals(spdf$weight,4)$brks)

I'm fairly sure I replied to this question very recently, but can't find 
the link. The at= argument in lattice graphics by default breaks [a, b), 
see:

http://en.wikipedia.org/wiki/Interval_(mathematics)#Excluding_the_endpoints

so the upper limit should be eased out beyond the maximum, here doing 
both for symmetry:

ats <- classIntervals(spdf$weight,4)$brks
ats[1] <- ats[1] - .Machine$double.eps
ats[length(ats)] <- ats[length(ats)] + .Machine$double.eps
spplot(spdf, "weight", at=ats)

This is described on the levelplot help page, linked from the spplot help 
page:

       at: A numeric vector giving breakpoints along the range of ‘z’.
           ...  However, this also means that when supplying ‘at’
           explicitly, one has to be careful to include values outside
           the range of ‘z’ to ensure that all the data are shown.

Hope this clarifies,

Roger

>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list