[R-sig-Geo] shapefiles plotting and colouring

Roger Bivand Roger.Bivand at nhh.no
Fri Oct 31 21:18:05 CET 2003


Paolo:

The key question will be to make sure that the colours you use to fill the 
polygons are in the correct order for the polygons. If you use Map2poly() 
to convert the shapes of a Map object to a polylist, you will see that you 
can attach a "region.id" attribute. When you access your database records, 
you could use that to check, and if necessary re-order the data frame rows 
using match(), to put them in the same order as the spatial objects (which 
can be multiple polygons). 

Then if your variable is a factor, you need to assign colours to its
levels, if continuous, you need to choose and impose class intervals to
use colour table lookup in the col= argument to plotpolys. Try running
example(plotpolys), and then just type for example cols[np] to see how the
table lookup is being used to feed a vector of colours of the same length
as mappolys to the function. The results of length(mappolys) and
length(cols[np]) should be the same. But I do recommend checking the
"region.id" against your database!

Hope this helps,

Roger


On Fri, 31 Oct 2003, Paolo Cavallini wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Thanks for this. My problem is, however, to use variables from a database 
> (PostgreSQL in our case) rather than those of the dbf associated with the shp 
> file.
> All the best.
> Paolo
> 
> At 18:03, venerdì 31 ottobre 2003 you presumably wrote:
> > Hi.
> >
> > I've tried to draw choropleth maps by rmap package as follows.
> >
> > library(rmap)
> >
> > mySF <- shapefile("columbus.shp")
> >
> >  crime <- mySF$CRIME
> >
> > # user-defined legend
> >  classes <- c(50,100,500,1000,5000)
> >  breaks <- length(classes) + 1
> >  classNo <- crime < classes[1]
> >  for(i in 1:(breaks - 1))
> >    classNo <- classNo + (crime >= classes[i] & crime < classes[i + 1]) * i
> >
> >  classNo <- classNo + (crime >= max(classes)) * breaks
> >  plot(mySF, f=gray(1 - classNo/breaks)) # draw map
> >  lines(mySF, col="red") # draw outlines
> >
> > ----- Original Message -----
> > From: "Paolo Cavallini" <cavallini at faunalia.it>
> > To: <r-sig-geo at stat.math.ethz.ch>
> > Sent: Friday, October 31, 2003 11:57 PM
> > Subject: [R-sig-Geo] shapefiles plotting and colouring
> >
> > > -----BEGIN PGP SIGNED MESSAGE-----
> > > Hash: SHA1
> > >
> > > Hi all.
> > > it may be a silly question, but: how can I, after reading a shapefile
> > > with
> >
> > the
> >
> > > appropriate library, draw them with different colours (or grey shades)
> > > according to different database fields (read through RODBC)? The idea is
> > > similar to the polygon command, but using shp as polygons and database
> >
> > fields
> >
> > > as qualifiers.
> > > Thanks for any suggestions.
> > > Ciao.
> > > paolo
> 
> - -- 
> Paolo Cavallini
> cavallini at faunalia.it
> www.faunalia.it
> Piazza Garibaldi 5
> 56025 Pontedera (PI)
> Italy
> Tel: (+39)348-3801953
> GPG signature/firma digitale @:
> www.faunalia.it/Public_key_Paolo.asc
> - ------------------------------------------
> Lavoro esclusivamente con software libero.
> I use exclusively free software.
> www.gnu.org / www.linux.org
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.3 (GNU/Linux)
> 
> iD8DBQE/opuc/NedwLUzIr4RApSMAJoCktLlzZVBfzStI09vsNKScgDk4wCdHNz8
> +HhE2grymbl4Z5S84M2Fwgo=
> =4H68
> -----END PGP SIGNATURE-----
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://www.stat.math.ethz.ch/mailman/listinfo/r-sig-geo
> 

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




More information about the R-sig-Geo mailing list