[R-sig-Geo] Help with simple Map of US states to predefined regions

MacQueen, Don m@cqueen1 @ending from llnl@gov
Fri Sep 14 01:51:34 CEST 2018


I know this is not a complete solution -- and it's a very different approach -- but it should at least show you one way to reliably get states colored by region.
I also left out Alaska and Hawaii.

require(sp)
require(rgdal)

## US Census Bureau Tiger file -- polygons of each US State
## try this URL for download
##   https://www.census.gov/cgi-bin/geo/shapefiles/index.php?year=2017&layergroup=States+%28and+equivalent%29

ustf <- readOGR('.', 'tl_2017_us_state', stringsAsFactors=FALSE)

## note, the Tiger file includes 6 additional territories
dim(ustf)
## [1] 56 14

## get rid of the extra six territories  (state.name comes with R)
cus <- subset(ustf, NAME %in% state.name)

## cheap rename
cus$state <- cus$NAME
cus$abb <- cus$STUSPS

## invent ridiculous groupings of states
cus$grp <- 'a'
cus$grp[11:20] <- 'b'
cus$grp[21:30] <- 'c'
cus$grp[31:40] <- 'd'
cus$grp[41:50] <- 'e'

## assign colors to the groups
cus$color <- 'red'
cus$color[cus$grp=='b'] <- 'green'
cus$color[cus$grp=='c'] <- 'blue'
cus$color[cus$grp=='d'] <- 'brown'
cus$color[cus$grp=='e'] <- 'cyan'

## exclude Alaska, Hawaii
cus <- subset(cus, !(state %in% c('Alaska','Hawaii')))

## get rid of extraneous variables (optional)
cus <- cus[ , c('state','REGION','abb', 'grp') ]

## plot colored by regions as defined in the Census Bureau Tiger file
plot(cus, col=cus$REGION, usePolypath=FALSE)
## color "1" is black, looks bad, do this instead
plot(cus, col=as.numeric(cus$REGION)+1, usePolypath=FALSE)
text(coordinates(cus), cus$abb, col='white', cex=0.75)

## colors specified by a color variable in the data
plot(cus, col=cus$color, usePolypath=FALSE)
text(coordinates(cus), cus$abb, col='white', cex=0.75)


--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 9/12/18, 11:27 AM, "R-sig-Geo on behalf of Bill Poling" <r-sig-geo-bounces using r-project.org on behalf of Bill.Poling using zelis.com> wrote:

    Hi
    
    I have this df with three columns ProviderState, ProviderStateCode, ProviderRegion I wanted to use to create a simple 5 color map
    
    I have reviewed fiftystater pkg and map pkg but not sure how to simply take these three columns and plot a simple 5 color map based on the Region the state is in?
    
    After looking at these and trying to apply these ideas to my data
    https://cran.r-project.org/web/packages/fiftystater/vignettes/fiftystater.html
    https://cran.r-project.org/web/packages/maps/maps.pdf
    
    I found tutorial at: https://uchicagoconsulting.wordpress.com/tag/r-ggplot2-maps-visualization/
    
    I used the tutorial data and subset in my regions
    
    So now I have come up with the 5 segmented maps and my question becomes how to put this all into one map of the US?
    
    
    install.packages("maps")
    library(maps)
    library(ggplot2)
    
    #load us map data
    all_states <- map_data("state") View(all_states)
    #plot all states with ggplot
    p <- ggplot()
    p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="white", fill="blue" )
    p
    
    #http://sape.inf.usi.ch/quick-reference/ggplot2/colour
    
    #Pacificstates
    Pacificstates <- subset(all_states, region %in% c( "alaska", "arizona", "california", "hawaii", "nevada", "oregon","washington") )
    p <- ggplot()
    p <- p + geom_polygon( data=Pacificstates, aes(x=long, y=lat, group = group),colour="white", fill="deepskyblue4" ) +
      labs(title = "Pacificstates")
    p
    
    #Frontierstates
    Frontierstates <- subset(all_states, region %in% c( "colorado", "idaho", "kansas", "montana", "new mexico", "oklahoma","texas", "utah", "wyoming") )
    p <- ggplot()
    p <- p + geom_polygon( data=Frontierstates, aes(x=long, y=lat, group = group),colour="white", fill="dodgerblue1" ) +
          labs(title = "FrontierStates")
    p
    
    #Midweststates
    Midweststates <- subset(all_states, region %in% c( "iowa", "illinois", "indiana", "michigan", "minnesota", "missouri","north dakota", "nebraska", "ohio","south dakota","wisconsin") )
    p <- ggplot()
    p <- p + geom_polygon( data=Midweststates, aes(x=long, y=lat, group = group),colour="white", fill="dodgerblue1" ) +
          labs(title = "MidwestStates")
    p
    
    #Southernstates
    Southernstates <- subset(all_states, region %in% c( "alabama", "arkansas", "florida", "georgia", "kentucky", "louisiana","mississippi"
                                                        ,"north carolina", "south carolina","tennessee","virginia","west virginia") )
    p <- ggplot()
    p <- p + geom_polygon( data=Southernstates, aes(x=long, y=lat, group = group),colour="white", fill="royalblue2" ) +
      labs(title = "Southernstates")
    p
    
    # Northeaststates
    Northeaststates <- subset(all_states, region %in% c( "connecticut", "district of columbia", "delaware", "massachusetts", "maryland", "maine","new hampshire"
                                                         , "new jersey", "new york","pennsylvania","rhode island","vermont") )
    p <- ggplot()
    p <- p + geom_polygon( data=Northeaststates, aes(x=long, y=lat, group = group),colour="white", fill="dodgerblue4" ) +
      labs(title = "Northeaststates")
    p
    
    
    #here is my my data but not used above
    
    str(Map1)
    Classes 'tbl_df', 'tbl' and 'data.frame':    54 obs. of  3 variables:
    $ ProviderState    : chr  "ALASKA" "ALABAMA" "ARKANSAS" "ARIZONA" ...
    $ ProviderStateCode: chr  "AK" "AL" "AR" "AZ" ...
    $ ProviderRegion   : chr  "Pacific" "South" "South" "Pacific" ...
    - attr(*, "spec")=List of 2
      ..$ cols   :List of 3
      .. ..$ ProviderState    : list()
      .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
      .. ..$ ProviderStateCode: list()
      .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
      .. ..$ ProviderRegion   : list()
      .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
      ..$ default: list()
      .. ..- attr(*, "class")= chr  "collector_guess" "collector"
      ..- attr(*, "class")= chr "col_spec"
    
    dput(Map1)
    structure(list(ProviderState = c("ALASKA", "ALABAMA", "ARKANSAS",
    "ARIZONA", "CALIFORNIA", "COLORADO", "CONNECTICUT", "DISTRICT OF COLUMBIA",
    "DELAWARE", "FLORIDA", "GEORGIA", "GUAM", "HAWAII", "IOWA", "IDAHO",
    "ILLINOIS", "INDIANA", "KANSAS", "KENTUCKY", "LOUISIANA", "MASSACHUSETTS",
    "MARYLAND", "MAINE", "MICHIGAN", "MINNESOTA", "MISSOURI", "MISSISSIPPI",
    "MONTANA", "NORTH CAROLINA", "NORTH DAKOTA", "NEBRASKA", "NEW HAMPSHIRE",
    "NEW JERSEY", "NEW MEXICO", "NEVADA", "NEW YORK", "OHIO", "OKLAHOMA",
    "OREGON", "PENNSYLVANIA", "PUERTO RICO", "RHODE ISLAND", "SOUTH CAROLINA",
    "SOUTH DAKOTA", "TENNESSEE", "TEXAS", "UTAH", "VIRGINIA", "VIRGIN ISLANDS",
    "VERMONT", "WASHINGTON", "WISCONSIN", "WEST VIRGINIA", "WYOMING"
    ), ProviderStateCode = c("AK", "AL", "AR", "AZ", "CA", "CO",
    "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN",
    "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT",
    "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR",
    "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VI", "VT",
    "WA", "WI", "WV", "WY"), ProviderRegion = c("Pacific", "South",
    "South", "Pacific", "Pacific", "Frontier", "Northeast", "Northeast",
    "Northeast", "South", "South", "Pacific", "Pacific", "Midwest",
    "Frontier", "Midwest", "Midwest", "Frontier", "South", "South",
    "Northeast", "Northeast", "Northeast", "Midwest", "Midwest",
    "Midwest", "South", "Frontier", "South", "Midwest", "Midwest",
    "Northeast", "Northeast", "Frontier", "Pacific", "Northeast",
    "Midwest", "Frontier", "Pacific", "Northeast", "Northeast", "Northeast",
    "South", "Midwest", "South", "Frontier", "Frontier", "South",
    "Northeast", "Northeast", "Pacific", "Midwest", "South", "Frontier"
    )), row.names = c(NA, -54L), class = c("tbl_df", "tbl", "data.frame"
    ), spec = structure(list(cols = list(ProviderState = structure(list(), class = c("collector_character",
    "collector")), ProviderStateCode = structure(list(), class = c("collector_character",
    "collector")), ProviderRegion = structure(list(), class = c("collector_character",
    "collector"))), default = structure(list(), class = c("collector_guess",
    "collector"))), class = "col_spec"))
    
    Thank you for any suggestions.
    
    WHP
    
    
    
    
    
    
    Confidentiality Notice This message is sent from Zelis. ...{{dropped:15}}
    
    _______________________________________________
    R-sig-Geo mailing list
    R-sig-Geo using r-project.org
    https://stat.ethz.ch/mailman/listinfo/r-sig-geo
    



More information about the R-sig-Geo mailing list