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

Lee Hachadoorian Lee.Hachadoorian+L at gmail.com
Sun Jun 16 20:21:37 CEST 2013


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


# 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
lwd = as.double(lwd) # This works!
plot(sldfTemp, lwd) # This still does not work



* 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

Lee Hachadoorian
Assistant Professor in Geography, Dartmouth College

More information about the R-sig-Geo mailing list