[R-sig-Geo] plotting polygons of one shapefile in different colors?

Maren Huck M.Huck at derby.ac.uk
Thu Sep 26 18:28:31 CEST 2013


Hi Cyndy (?),
I am right now in a similar process, and I came up with this code for my dataset:
If you use the shapefile directly (not via spplot):
"habit" in this case is the name of the shapefile.
"GRIDCODE" is the column which identifies the different habitat types (polygons)
I then create a new attribute for the shapefile with the third code line. You would obviously have to change the word "habit", but everything else stays as it is.
The following lines then assign a colour to each "GRIDCODE" (the GRIDCODE is a number from 0 to 10).
And the final line plots the map.

habit$GRIDCODE<-as.factor(habit$GRIDCODE)
habit at data$COLOUR <- "#FFFFFF"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 0] <- "grey"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 1] <- "green2"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 1] <- "green2"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 2] <- "darkseagreen"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 4] <- "pink"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 5] <- "darkgreen"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 6] <- "lightblue"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 7] <- "green"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 8] <- "blue"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 9] <- "navy"
habit at data$COLOUR[(as.numeric(as.character(habit at data$GRIDCODE)) %% 10) == 10] <- "orange"
plot(habit, col=habit$COLOUR, add=T)


If you want to use spplot, its slightly different:
I first create a vector with colour names. The "Helen_trans" bit in the second line is are spatial points (radiolocations of a badger)
code<-c("grey", "green2", "darkseagreen", "pink", "darkgreen", "lightblue", "green", "blue", "navy", "orange")
spplot(habit, zcol="GRIDCODE", col.regions=code, sp.layout=list("sp.points", Helen_trans, pch=16, col="blue"))

I hope this helps.
Maren

Maren Huck
Dep. of Biological and Forensic Sciences
University of Derby
Kedleston Road
Derby DE22 1GB
U.K.
Tel: +44-(0)1332-59 2354


-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of cyndy-jer
Sent: 26 September 2013 17:04
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] plotting polygons of one shapefile in different colors?

Hi folks -

Pardon some real rank newbie questions; I am very well versed in using ArcGIS but something of a newbie to R, and just starting out doing GIS in R.  

I've installed sp, rgdal, and maptools, and now I'm trying to get some mapping done!  

I've been able to load a type of shapefile I am currently in need of working with.  It is a set of polygons, that are grouped into different strata that may or may not be contiguous.  There is a field in the shapefile that denotes which polygons are grouped together.  I hope it is clear what I have?  

I can readily plot the entire polygon shapefile; what I have not been able to work out is, how to access the field in the shapefile that groups them,
and plot each polygon group in a different color.   In ArcGIS, my shapefile
has a field named 'n' that represents the grouping.  How would I go about making a map that shows my polygons grouped by 'n' in different colors? 

Thanks in advance for any pointers!





--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/plotting-polygons-of-one-shapefile-in-different-colors-tp7584720.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_____________________________________________________________________
The University of Derby has a published policy regarding email and reserves the right to monitor email traffic. If you believe this email was sent to you in error, please notify the sender and delete this email. Please direct any concerns to Infosec at derby.ac.uk.



More information about the R-sig-Geo mailing list