[R] Plot by FIPS Code using Shapefiles

Shouro Dasgupta shouro at gmail.com
Thu May 7 16:23:36 CEST 2015


Excellent suggestions Professors! I really appreciated it. This is what I
am using:

library(data.table)
> library(rgdal)
> library(colourschemes)
> library(RColorBrewer)
> library(maptools)
> library(maps)
> library(ggmap)
> library(classInt)


max_change is my csv file while shapes is my spatial data:

max_change <-read.csv ("F:/GCM/max_change.csv")
> max_change <- as.data.table(max_change)
> max_change$FIPS <- sprintf("%05d",as.numeric(max_change$FIPS))
>


> max_change <- max_change[with(max_change, order(FIPS)), ]
>


> shapes=readOGR(dsn="F:/GCM//tl_2014_us_county", "tl_2014_us_county")
> shapes$FIPS = paste(shapes$STATEFP,shapes$COUNTYFP,sep="")
>


> m = match(shapes$GEOID,max_change$FIPS)
>


> shapes$change = max_change$change[m]


I am plotting the results now. I am getting different plots regarding
county boundaries (most likely because I am doing something wrong). When I
use the *plot *command:

colours = brewer.pal(6,"PuRd")
> sd = data.frame(col=colours,values=c(-2.5,-2,-1.5,-1,-.5,0))
> sc = nearestScheme(sd)
>


> plot(c(-129,-61),c(21,53),type="n",axes=FALSE,xlab="",ylab="")
> title(main = "Mid-Century Projections (GISS-ER-2) using Max GAM")
> plot(shapes,col=sc(shapes$change),add=TRUE,border="white",lwd=0.2,
> colorkey=T)


I get a "beautiful" plot with the county boundaries clearly separated but
for *spplot*;

pal = brewer.pal(6,"Reds")
> brks.eq = classIntervals(shapes$change, style = "jenks")
> spplot(shapes, "change",xlim = c(-129,-61), ylim = c(21,53),
> at=brks.eq$brks,col.regions=pal, col="transparent",
>        main = list(label="Mid-Century Projections (GISS-ER-2) using Max
> GAM"))


 The county boundaries/FIPS are not defined. What am I doing wrong? Thanks
again!

Sincerely,

Shouro

On Wed, May 6, 2015 at 12:23 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:

> Corey Sparks <corey.sparks <at> utsa.edu> writes:
>
> >
> > Joining data the way you're doing it is dangerous, Roger Bivand and
> others
> > describes a standard way to do this process here:
> >
>
> http://r-sig-geo.2731867.n2.nabble.com/Merging-shapefiles-and-csv-td7586839.html
>
>
> Quite right - the chunks Corey is referring to are:
>
> Please do refer to the vignette in the maptools package, and to previous
> threads which have advised that merge() should not be used, and that the
> row.names of the data frames be used as ID keys. Typically using match() on
> the row.names of the two objects will show which are not correctly aligned.
>
> and
>
> Beware that the data from the objects may be jumbled - never use merge,
> always use match() on the row.names vectors of the objects to ensure that
> the key-IDs agree. Jumbled data happens, it is important not to think
> "shapefile" but to think DBMS with the ID key your way of staying sane.
>
> The maptools vignette is at:
>
>
> http://cran.r-project.org/web/packages/maptools/vignettes/combine_maptools.pdf
>
> or:
>
> library(maptools)
> vignette("combine_maptools")
>
> Here I also suspect that you'll find that there are non-unique FIPS in the
> county polygons file, so may need to go through
> maptools::unionSpatialPolygons() first.
>
> Roger
>
> >
> > And I do an example using US Census data here, using merge():
> > http://spatialdemography.org/wp-content/uploads/2013/04/9.-Sparks.pdf
> > <http://spatialdemography.org/wp-content/uploads/2013/04/9.-Sparks.pdf>
> >
> > look at page 134 of that pdf.
> >
> > Hope this helps
> >
> > -----
> > Corey Sparks, PhD
> > Assistant Professor
> > Department of Demography
> > University of Texas at San Antonio
> > 501 West César E. Chávez  Blvd
> > Monterey Building 2.270C
> > San Antonio, TX 78207
> > 210-458-3166
> > corey.sparks 'at' utsa.edu
> > coreysparks.weebly.com
> > --
> > View this message in context:
>
> http://r.789695.n4.nabble.com/Plot-by-FIPS-Code-using-Shapefiles-tp4706830p4706840.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > R-help <at> r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 

Shouro Dasgupta
PhD Candidate | Department of Economics
Ca' Foscari University of Venezia

------------------------------

Junior Researcher | Fondazione Eni Enrico Mattei (FEEM)
Isola di San Giorgio Maggiore, 8 | 30124 Venice, Italy
Tel: +39 041 2700 436

	[[alternative HTML version deleted]]



More information about the R-help mailing list