Jan Hornych
2013-Jan-04 17:36 UTC
[R] SpatialPolygon with the max value gets no color assigned in spplot function when using "at" parameter
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 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)) spdfSpatialPolygonsDataFrame(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) [[alternative HTML version deleted]]
Roger Bivand
2013-Jan-05 14:16 UTC
[R] SpatialPolygon with the max value gets no color assigned in spplot function when using
Jan Hornych <jh.hornych <at> gmail.com> writes:> > 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 coloring all regions within the interval with the same color > > col = brewer.pal(4,"RdYlGn")) >If you look at the links from ?spplot, then ?levelplot explains what is happening: at: A numeric vector giving breakpoints along the range of ?z?. Contours (if any) will be drawn at these heights, and the regions in between would be colored using ?col.regions?. In the latter case, values outside the range of ?at? will not be drawn at all. This serves as a way to limit the range of the data shown, similar to what a ?zlim? argument might have been used for. 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. When using base graphics, the classInt package takes care to let the user specify how to define the breakpoints (via arguments to findIntervals), but no provision is made for spplot or lattice graphics as such. Consequently, at present you need to add a very small value to the top breakpoint, and subtract from the bottom for symmetry: brks <- classIntervals(spdf$weight,4)$brks eps <- .Machine$double.eps brks[1] <- brks[1] - eps brks[length(brks)] <- brks[length(brks)] + eps spplot(spdf, "weight", at=brks) for some suitable value of eps. Note that the default class intervals are by quantile. Please consider the R-sig-geo list for questions of this kind - thank you for including a simple working example. Hope this clarifies, Roger