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

Lee Hachadoorian Lee.Hachadoorian+L at gmail.com
Sun Jun 16 20:59:01 CEST 2013


On 06/16/2013 02:47 PM, Roger Bivand wrote:
> 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)

Embarrassingly, yes.

I suppose that's a danger of naming variables to match argument names. I 
doubt I would have made that mistake if I used a different variable name.

Thank you,
--Lee

-- 
Lee Hachadoorian
Assistant Professor in Geography, Dartmouth College
http://freecity.commons.gc.cuny.edu



More information about the R-sig-Geo mailing list