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

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


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

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



More information about the R-sig-Geo mailing list