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

Bill Poling Bill@Poling @ending from zeli@@com
Wed Sep 12 20:27:13 CEST 2018


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}}



More information about the R-sig-Geo mailing list