[R-sig-Geo] ?Plotting line segments of shapefile in different color

Alexander.Herr at csiro.au Alexander.Herr at csiro.au
Thu Feb 2 03:27:25 CET 2006


Oops, option b should read

b)use Map2lines and run a for loop on the length of the Map2lines object
and circle through the colors
library(maptools)
fn<-system.file("shapes/fylk-val.shp", package="maptools") 
read.shape(fn)->xx
Map2lines(xx)->s3
xylims<-attr(s3, "maplim")
plot(xylims$x, xylims$y, asp=1, type='n', xlab="", ylab="") 
cols1<-c("green", "blue", "black", "red") 
j=0 
for (i in 1:length(s3)) {
   if (j < 4) j = j+1 else j = 1
  lines(s3[[i]], col=cols1[j])
 }
 
Herry

-----Original Message-----
From: Herr, Alexander Herr - Herry (CSE, Townsville) 
Sent: Thursday, 2 February 2006 12:21 PM
To: 'r-sig-geo at stat.math.ethz.ch'
Cc: 'Roger.Bivand at nhh.no'
Subject: RE: [R-sig-Geo] ?Plotting line segments of shapefile in
different color

Thanks Roger,

ColorRampPalette only allows for 255 colors, but since I want to show
only the difference between each adjacent segment, a few colors will do.
So the way to do this is

a) use readShapelines and repeat the color listing for the number of
line segments (ie length(slot(s2,"lines")/number of colors) and plot the
readShapeline object
library(maptools)
fn<-system.file("shapes/fylk-val.shp", package="maptools")
s2 <- readShapeLines(fn, proj4string=CRS("+proj=utm +zone=33
+datum=WGS84"))
length(slot(s2,"lines"))->len1
rep(c("green", "blue", "black", "red"), len=len1)->cols1 plot(s2,
col=cols1)


b)use Map2lines and run a for loop on the length of the Map2lines object
and circle through the colors
library(maptools)
fn<-system.file("shapes/fylk-val.shp", package="maptools")
read.shape(fn)->xx
Map2lines(xx)->s3
xylims<-attr(s3, "maplim")
plot(xylims$x, xylims$y, asp=1, type='n', xlab="", ylab="")
cols1<-c("green", "blue", "black", "red") j=0 for (i in 1:length(s3)) {
  if (j < 5) j = j+1 else j = 0
   lines(s3[[i]], col=cols1[j])
 }


Cheers
Herry
 
-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: Wednesday, 1 February 2006 5:36 PM
To: Herr, Alexander Herr - Herry (CSE, Townsville)
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] ?Plotting line segments of shapefile in
different color

On Wed, 1 Feb 2006 Alexander.Herr at csiro.au wrote:

> Hi List,
> 
> I am unsuccessfully trying to plot line segments of a shapefile with 
> different colors. Does anyone have a working example for segmented 
> lines?

If each line to be coloured is a separate Lines object:

library(maptools)
fn <- system.file("shapes/fylk-val.shp", package="maptools")[1] xx <-
readShapeLines(fn, proj4string=CRS("+proj=utm +zone=33 +datum=WGS84"))
plot(xx, col="blue") # all one colour length(slot(xx, "lines")) # 97
plot(xx, col=colorRampPalette(c("green", "blue", "black"))(97))
length(colorRampPalette(c("green", "blue", "black"))(97)) # 97 colours

The col= argument is used if it is the same length as the number of
lines; if the lengths differ, the first colour value is used for all
lines.

If the shapes are not organised as separate Lines objects, then they
will need to be cut up and re-created as such.

Roger


> 
> Thanks
> Herry
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list