Skip to content

Drawing lines in raster cells

6 messages · Oscar Perpiñan, Johannes Signer, Robert J. Hijmans +1 more

#
Hi Lionel,

With this code you will draw lines connecting raster cells with a
certain value. I am not sure if you also need to build a SpatialLines
object with them (which you won't get with my code):

library(raster)
library(latticeExtra) ## to use +.trellis and layer()

r1<-raster(ncols=10,nrows=10)
r1[]<-sample(0:1,100,replace=TRUE)

## extract coordinates of those cells with a condition
myCells <- Which(r1==1, cells=TRUE)
xy <- xyFromCell(r1, myCells)

## border and border.lwd are only needed to highlight the cells boundaries
spplot(r1, at=c(0, 0.5, 1), border='black', border.lwd=0.3) +
    layer(llines(xy, type='b', col='black'))


Best,

Oscar.

Lionel <s6lihert at uni-bonn.de> writes:

  
    
#
Hi Oscar,

Thanks for your help, what I would like to get is something similar to this:
http://www.google.fr/imgres?q=species+range+shift&start=280&um=1&sa=N&hl=fr&biw=1366&bih=647&tbm=isch&tbnid=MdhZVMKVLMSH3M:&imgrefurl=http://www.scielo.sa.cr/scielo.php%3Fscript%3Dsci_arttext%26pid%3DS0034-77442008000200028&docid=9u3QDKVd0hdQqM&imgurl=http://www.scielo.sa.cr/img/revistas/rbt/v56n2/art28i2.jpg&w=363&h=432&ei=cbiPUe_4BqO80QX-hoCIBw&zoom=1&iact=hc&vpx=588&vpy=170&dur=342&hovh=180&hovw=152&tx=83&ty=124&page=10&tbnh=159&tbnw=134&ndsp=30&ved=1t:429,r:84,s:200,i:256

Where each raster cells with a certain values (say 1) get crossed by a 
diagonal line.

Your code is achieving something different, concerning the object 
created the best would be a Spatial* object, however I think I could be 
able to find a way around using ggmap.

Thanks again,
Lionel
On 12/05/2013 16:23, Oscar Perpi??n Lamigueiro wrote:
#
Lionel, perhaps this is what you are after. Robert

library(raster)
library(rgeos)

r <- raster(ncols=10,nrows=10)
set.seed(0)
r[] <- sample(0:1,100,replace=TRUE)

p <- rasterToPolygons(r, fun=function(x){x==1}, dissolve=TRUE)

plot(r)
plot(p, add=T, density=4)
On Sun, May 12, 2013 at 8:49 AM, Lionel <s6lihert at uni-bonn.de> wrote:
#
Yes, this is it, thank you very much for your help.
Lionel
On 13/05/2013 18:42, Robert J. Hijmans wrote: