[R-sig-Geo] contour line labels in trellis contourplot

Roger Bivand Roger.Bivand at nhh.no
Sun Jul 22 15:07:52 CEST 2012


On Sun, 22 Jul 2012, Martin Ivanov wrote:

> Hello,
> In the classical function "contour" labels are drawn so that each contour 
> line is broken at the place of the label,
>  so that the label does not overlap the line. In the trellis contourplot the 
> labels are placed beside or over the line,
>  controlled by the label.style parameter, but I could not find
> an option that would place the labels just as in the classical contour: 
> breaking the contour line to make place for
>  the label.

This would be so much easier if you did give a working example. Please 
always do so.

library(sp)
library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
proj4string(meuse.grid) <- CRS("+init=epsg:28992")
spplot(meuse.grid, "dist", col.regions=bpy.colors(20))
library(maptools)
lns <- ContourLines2SLDF(contourLines(
   as.image.SpatialGridDataFrame(meuse.grid["dist"]),
   nlevels=4), proj4string=CRS(proj4string(meuse.grid)))
spplot(meuse.grid, "dist", col.regions=bpy.colors(20),
   sp.layout=list("sp.lines", lns, lwd=2, col="grey", lty=2))
SLMP <- SpatialLinesMidPoints(as(lns, "SpatialLines"))
# here there is only one Line object per Lines object, so this works
spplot(meuse.grid, "dist", col.regions=bpy.colors(20),
   sp.layout=list(list("sp.lines", lns, lwd=2, col="grey", lty=2),
   list("sp.text", coordinates(SLMP), levels(lns$level), col="grey")))
# in other cases more care will be needed to assign multiple labels to 
# Lines objects.

The contour and contourLines functions do not report label placing points. 
You may extend this to add a background behind the labels if you like. The 
alternative with latticeExtra ought also to be tried.

Roger

>
> Have You got any ideas how breaking the contours to make place for the labels 
> could be achieved in a trellis contourplot?
>
> Best regards,
>
> Martin
>
>
>
>
>

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