[R-sig-Geo] Choropleth values plotting incorrectly
Roger Bivand
Roger.Bivand at nhh.no
Sat Jul 20 16:44:31 CEST 2013
On Fri, 19 Jul 2013, Justine Sumrok wrote:
> Hi all,
>
> I ran into a spot of trouble today plotting a map of free and reduced lunch
> students (FRL) in Ohio. linked are:
> https://drive.google.com/folderview?
> id=0ByHQd1ckVHrSTURXeU5MczgzWDQ&usp=sharing
>
> shapefile of school districts in Ohio
> csv of the district data
>
> Using the code below I created a choropleth map of Ohio, though the fill in
> certain school districts was incorrect. For example: according to my csv of
> data Cleveland Municipal would be in the highest bracket of FRL population,
> but is not displaying that color. Any thoughts?
Thanks for example data. Never use merge(), it always masks trouble. Never
assign directly to the data slot, always use spCbind() in maptools. You
have multiple problems. You have 616 entities in your shapefile, but only
601 of these are in the CSV file (which has 768 observations) - this based
on using match() on character representations of NCESID - it is a factor
in OH, but integer in dist. So you have a two-way matching problem. You
can maybe address it by first removing the 15 entities from OH for which
you have no data, then dropping from dist the IDs that do not match, and
finally spCbind() after checking that row.names(OH) and row.names(dist)
agree. I started by:
row.names(OH) <- as.character(OH$NCESID)
dist$NCESID <- as.character(dist$NCESID)
row.names(dist) <- dist$NCESID
to remove the possibility that the different representations were getting
in the way. I don't know what you'll choose to do with the IDs in OH foe
which you have no data, so I'll leave this here for now.
See the vignette("combine_maptools") in maptools for more ideas.
Hope this helps,
Roger
>
> #Libraries
> library(maps)
> library(maptools)
> library(RColorBrewer)
> library(class)
> library(classInt)
>
> #Reading in data
> state <- readShapePoly('/Users/Path/to/File/Census2010')
> dist <- read.csv('/Users/Path/to/File/Dist.csv', header = T)
>
> #Merging Data
> OH <- state
> OH at data <-
> merge(OH at data, dist, by.x="NCESID", by.y="NCESID", all.x=F, sort=F )
>
> #Plotting FRL by district
> plotvar <- OH at data$PctFRL
> nclr <- 5
> plotclr <- brewer.pal(nclr, "GnBu")
> class <- classIntervals(plotvar, nclr, style = "fixed",
> fixedBreaks = c(0, 0.40, 0.60, 0.80, 0.90, 1))
> legend("bottomleft", legend=names(attr(colcode, "table")),
> fill=attr(colcode, "palette"), cex=0.6, bty="n")
>
> Thanks!
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> 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: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list