plotting polygons of one shapefile in different colors?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

plotting polygons of one shapefile in different colors?

cyndy-jer
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!

Reply | Threaded
Open this post in threaded view
|

Re: plotting polygons of one shapefile in different colors?

MarenHuck
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@data$COLOUR <- "#FFFFFF"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 0] <- "grey"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 1] <- "green2"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 1] <- "green2"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 2] <- "darkseagreen"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 4] <- "pink"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 5] <- "darkgreen"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 6] <- "lightblue"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 7] <- "green"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 8] <- "blue"
habit@data$COLOUR[(as.numeric(as.character(habit@data$GRIDCODE)) %% 10) == 9] <- "navy"
habit@data$COLOUR[(as.numeric(as.character(habit@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: [hidden email] [mailto:[hidden email]] On Behalf Of cyndy-jer
Sent: 26 September 2013 17:04
To: [hidden email]
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
[hidden email]
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 [hidden email].

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: plotting polygons of one shapefile in different colors?

cyndy-jer
This post was updated on .
Hi Maren -

(yes I'm Cyndy :) )

Well, thanks, but yikes!  Surely there is an easier way?  I've gotten pretty close by trying to use spplot and rColorBrewer, but I'm not quite there.  I have 3 'n' values as part of my SpatialPolygonDataFrame, and somehow the below only plots 2 of them,

(my dataframe is named 'gimmie')

numpoly=length(unique(gimmie$n))

# the below yields a plot of three colors
display.brewer.pal(numpoly, "Accent")

#the below yields my map but with only two of the three above colors, yet the scale bar shows
#three colors with the three values I KNOW are in my 'n' values in the shapefile
spplot(gimmie, c("n"), xlim=c(minx,maxx), ylim=c(miny,maxy), col.regions=brewer.pal(numpoly, "Accent"))

? I don't see why this doesn't work as I expect.  Any ideas, anyone?
Reply | Threaded
Open this post in threaded view
|

Re: plotting polygons of one shapefile in different colors?

Roger Bivand
Administrator
On Thu, 26 Sep 2013, cyndy-jer wrote:

> Hi Maren -
>
> (yes I'm Cyndy :)
>
> Well, thanks, but yikes!  Surely there is an easier way?  I've gotten pretty
> close by trying to use spplot and rColorBrewer, but I'm not quite there.  I
> have 3 'n' values as part of my SpatialPolygonDataFrame, and somehow the
> below only plots 2 of them,

The easier way is to get to know and like factors, categorical variables
typically with few levels:

library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
class(meuse.grid$ffreq)
library(RColorBrewer)
spplot(meuse.grid, "ffreq1",
  col.regions=brewer.pal(nlevels(meuse.grid$ffreq), "Accent"))

but if the plotted variable is numeric (or integer):

meuse.grid$ffreq1 <- as.integer(meuse.grid$ffreq)
spplot(meuse.grid, "ffreq1",
col.regions=brewer.pal(nlevels(meuse.grid$ffreq), "Accent"))

you get trouble because at= by default splits the range of the variable
into 20, cycling the colours. You can fix at=

spplot(meuse.grid, "ffreq1", at=seq(0.5, 3.5, 1),
  col.regions=brewer.pal(nlevels(meuse.grid$ffreq), "Accent"))


but if your variable is really categorical, convert it to factor
representation, and live gets easier.

Roger

>
> (my dataframe is named 'gimmie')
>
> numpoly=length(unique(gimmie$n))
>
> # the below yields a plot of three colors
> display.brewer.pal(numpoly, "Accent")
>
> #the below yields my map but with only two of the three above colors, yet
> the scale bar shows
> #three colors with the three values I KNOW are in my 'n' values in the
> shapefile
> spplot(gimmie, c("n"), xlim=c(minx,maxx), ylim=c(miny,maxy),
> col.regions=brewer.pal(numpoly, "Accent"))
>
> ? I don't see why this doesn't work as I expect.  Any ideas, anyone?
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/plotting-polygons-of-one-shapefile-in-different-colors-tp7584720p7584727.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: plotting polygons of one shapefile in different colors?

cyndy-jer
Hi, Roger -

Thank you for your reply!  I now have the map I needed.

By the way, I've been finding your 2007 course materials most helpful to learning spatial data manipulation in R.

 

Cyndy
Reply | Threaded
Open this post in threaded view
|

Re: plotting polygons of one shapefile in different colors?

Roger Bivand
Administrator
On Thu, 26 Sep 2013, cyndy-jer wrote:

> Hi, Roger -
>
> Thank you for your reply!  I now have the map I needed.
>
> By the way, I've been finding your 2007 course materials most helpful to
> learning spatial data manipulation in R.
>

Please also see:

http://www.edii.uclm.es/~useR-2013/Tutorials/Bivand.html
http://www.edii.uclm.es/~useR-2013/Tutorials/bivand/useR13_bivand_tut.zip

also linked from:

http://www.asdar-book.org/courses.php

Roger

>
>
> Cyndy
>
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/plotting-polygons-of-one-shapefile-in-different-colors-tp7584720p7584730.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway