[R-sig-Geo] Trouble creating classed line widths

Roger Bivand Roger.Bivand at nhh.no
Sun Jun 16 20:47:07 CEST 2013


On Sun, 16 Jun 2013, Lee Hachadoorian wrote:

> Hello,
>
> I'm trying to map a SpatialLinesDataFrame where line weight is 
> data-dependent. I can produce an unclassed map but not a classed map. 
> findInterval() returns a vector, but when I try to assign it to lwd I get 
> "cannot coerce type 'S4' to vector of type 'double'", even though docs say 
> findInterval() returns a vector and typeof indicates integer (which should be 
> coerceable).
>
> # BEGIN
> library(sp)
> library(classInt)
>
> # Provide your own SLDF, see below*
> sldfTemp = sldfYourData
> sldfTemp$x = sldfTemp$YourVariable
>
> #Create dummy data
> sldfTemp$x = runif(length(sldfTemp$x), 1, 7)
>
> # All same weight.
> plot(sldfTemp, lwd=3) # This works
>
> # Unclassed, line weight proportional to data value.
> plot(sldfTemp, lwd=sldfTemp$x) # This works
>
> # Recycled vector, arbitrary.
> plot(sldfTemp, lwd=c(1,3,7)) # This works
>
> #Create class intervals
> ciX = classIntervals(sldfTemp$x, n=5, style="quantile")
>
> # Classed, line weight equals class index.
> lwd = findInterval(sldfTemp$x, head(ciX$brks, n=-1))
> plot(sldfTemp, lwd) # This does not work
> typeof(lwd)
> lwd = as.double(lwd) # This works!
> typeof(lwd)
> plot(sldfTemp, lwd) # This still does not work

Would using the argument name help?

library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
fullgrid(meuse.grid) <- TRUE
cL <- contourLines(as.image.SpatialGridDataFrame(meuse.grid["dist"]))
library(maptools)
SPcL <- ContourLines2SLDF(cL)
SPcL$x <- as.numeric(as.character(SPcL$level))
ciX = classIntervals(SPcL$x, n=5, style="quantile")
lwd = findInterval(SPcL$x, head(ciX$brks, n=-1))
# plot(SPcL, lwd)
# Error in as.double(x) :
#  cannot coerce type 'S4' to vector of type 'double'
plot(SPcL, lwd=lwd)

>
> # END
>
> Best,
> --Lee
>
> * Apologies for not providing a dataset. I tried to run code directly from 
> ASDAR which for some reason did not work, so I could not build a test SLDF 
> programmatically.
>
>> data(meuse)
>> coordinates(meuse) = c("x", "y")
>> plot(meuse)
>> cc = coordinates(meuse)
>> m.sl = SpatialLines(list(Lines(list(Line(cc)))))
> Error in Lines(list(Line(cc))) : Single ID required

See http://www.asdar-book.org -> Errata -> Chapter 3, or:

http://www.asdar-book.org/errata.php?errchapter=4

Roger

>
>

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