Skip to content

Trouble creating classed line widths

3 messages · Lee Hachadoorian, Roger Bivand

#
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

# 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
#
On Sun, 16 Jun 2013, Lee Hachadoorian wrote:

            
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)
See http://www.asdar-book.org -> Errata -> Chapter 3, or:

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

Roger

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