[R-sig-Geo] problem with findInterval when mapping in a loop

Alessandra Carioli alessandracarioli at gmail.com
Wed Apr 2 18:28:58 CEST 2014


Dear Bloggers,
I’ve been working with a for loop to create maps of dependent variables to speed up a bit the work but I keep getting this error message

Error in findInterval(varofint2, bins, rightmost.closed = T) :  'vec' must be sorted non-decreasingly

I am not sure what to do (I have tried sorting the variables, just to see what happens but I still get the message).
Any help on the matter is very much appreciated!

Ale

R code:

library(spdep)
library(RColorBrewer)

var.data<- data.frame(var1,var2,var3,var4,var5,var6,var7,var8,var9) 
var.names<-c(“name variable 1”,…,”name variable 9"

m <- length(var.data)
var.color <- matrix(nrow=n,ncol=m, data=rep(0,n*m))
for (i in 1:m) {
 varofint <- var.data[,i]
 mean <- mean(varofint)
 sd <- sd(varofint)
 min <- min(varofint)
 max <- max(varofint)
 bins <- c(min,mean-sd, mean, mean+sd, max)
 lb <- length(bins)
 med <- median(1:lb)
 colpal <- brewer.pal(lb, "PRGn")[-med]
 var.color[,i] <- colpal[findInterval(varofint, bins, rightmost.closed=T)] #this is what does not work
 plot(shape.shp, col=var.color[,i], bg="grey5",lty=0) #shape.shp name of shape file
 title(var.names[[i]], cex=1)
 legend("bottomright",fill=colpal,legend=paste(round(bins[-length(bins)],2),
                                               "-", round(bins[-1],2)),cex=0.7, bg="white")
}


More information about the R-sig-Geo mailing list