[R] Plotting US States: Issue with filled colors cycling
David L Carlson
dcarlson at tamu.edu
Thu Sep 13 05:51:03 CEST 2012
The map() function plots polygons. Since several states have noncontiguous
parts, there are actually 63 polygons plotted:
> statepolys <- map("state", namesonly=TRUE, plot=FALSE)
> statepolys
The following commands produce the map after merging the color values in
dState to the list of polygons:
> dState = data.frame( col=rbinom(51,size=2,prob=.6)+1, state=tolower(state)
)
> statepolys <- do.call(rbind, strsplit(statepolys, ":"))[,1]
> statepolys <- data.frame(state=statepolys)
> statepolys <- merge(statepolys, dState)
> map("state", fill=TRUE, col=statepolys$col, resolution=0)
With the default (resolution=1), there are some gaps in the shared
boundaries between a few states.
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Kevin Wright
> Sent: Wednesday, September 12, 2012 9:21 PM
> To: Josh Browning
> Cc: r-help at r-project.org
> Subject: Re: [R] Plotting US States: Issue with filled colors cycling
>
> Even this one-liner shows Michigan with 2 colors.
> map("state", col=1:3, fill=TRUE)
>
> This link might help:
> http://tolstoy.newcastle.edu.au/R/devel/06/08/6354.html
>
> Kevin
>
>
> On Wed, Sep 12, 2012 at 10:38 AM, Josh Browning
> <rockclimber112358 at gmail.com> wrote:
> > I'm having issues when trying to do a filled State plot using the
> maps()
> > package. It seems that the color values aren't being plotted
> correctly,
> > and I'm not sure why...
> >
> > I've started debugging it, but I'm stuck. I've attached the code
> that I've
> > got so far. The for loop plots states 1:i. What I'm noticing is
> that the
> > colors are correct until after Michigan gets plotted (i=24). Then,
> the
> > colors change each time the for loop is executed. I'm guessing it
> has
> > something to do with Michigan having multiple regions, but I have no
> idea
> > what to do from here. Help! (and thanks for taking the time to read
> > this/help me out)
> >
> > state = c("Alabama","Alaska","Arizona","Arkansas",
> > "California","Colorado","Connecticut","Delaware",
> > "District Of Columbia","Florida","Georgia",
> > "Hawaii","Idaho","Illinois","Indiana",
> > "Iowa","Kansas","Kentucky","Louisiana",
> > "Maine","Maryland","Massachusetts",
> > "Michigan","Minnesota","Mississippi","Missouri",
> > "Montana","Nebraska","Nevada","New Hampshire",
> > "New Jersey","New Mexico","New York","North Carolina",
> > "North Dakota","Ohio","Oklahoma","Oregon",
> > "Pennsylvania","Rhode Island","South Carolina","South Dakota",
> > "Tennessee","Texas","Utah","Vermont",
> > "Virginia","Washington","West Virginia","Wisconsin","Wyoming")
> >
> > dState = data.frame( col=rbinom(51,size=2,prob=.6)+1, state=state )
> >
> > for( i in 1:51 ){
> > dTemp = dState[1:i,]
> > map("state", regions = dTemp$state,
> > # boundary = TRUE,
> > # lty = 1, lwd =1,
> > col= dTemp$col,
> > fill=T)
> > readline( paste( "Current i is", i ) )
> > }
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > 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.
>
>
>
> --
> Kevin Wright
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
More information about the R-help
mailing list