Help with simple Map of US states to predefined regions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with simple Map of US states to predefined regions

Bill Poling
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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Help with simple Map of US states to predefined regions

R-sig-geo mailing list
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" <[hidden email] on behalf of [hidden email]> 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
    [hidden email]
    https://stat.ethz.ch/mailman/listinfo/r-sig-geo
   

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Help with simple Map of US states to predefined regions

Jérome Mathieu
In reply to this post by Bill Poling
Hi,

this might help you:

http://socviz.co/maps.html#maps
http://strimas.com/r/tidy-sf/
https://cran.r-project.org/web/packages/fiftystater/vignettes/fiftystater.html

Jerome





Le mer. 12 sept. 2018 à 20:27, Bill Poling <[hidden email]> a écrit :
>
> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Help with simple Map of US states to predefined regions

Bill Poling
Thank you Jerome, I will peruse and let you know.

Best regards
WHP


From: Jérome Mathieu <[hidden email]>
Sent: Friday, September 14, 2018 3:37 AM
To: Bill Poling <[hidden email]>
Cc: r-sig-geo <[hidden email]>
Subject: Re: [R-sig-Geo] Help with simple Map of US states to predefined regions

Hi,

this might help you:

http://socviz.co/maps.html#maps<http://socviz.co/maps.html#maps>
http://strimas.com/r/tidy-sf/<http://strimas.com/r/tidy-sf/>
https://cran.r-project.org/web/packages/fiftystater/vignettes/fiftystater.html<https://cran.r-project.org/web/packages/fiftystater/vignettes/fiftystater.html>

Jerome





Le mer. 12 sept. 2018 à 20:27, Bill Poling <[hidden email]<mailto:[hidden email]>> a écrit :

>
> 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/fiftystater/vignettes/fiftystater.html>
> https://cran.r-project.org/web/packages/maps/maps.pdf<https://cran.r-project.org/web/packages/maps/maps.pdf>
>
> I found tutorial at: https://uchicagoconsulting.wordpress.com/tag/r-ggplot2-maps-visualization/<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<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
> [hidden email]<mailto:[hidden email]>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>



Confidentiality Notice This message is sent from Zelis. This transmission may contain information which is privileged and confidential and is intended for the personal and confidential use of the named recipient only. Such information may be protected by applicable State and Federal laws from this disclosure or unauthorized use. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, review, discussion, copying, or taking any action in reliance on the contents of this transmission is strictly prohibited. If you have received this transmission in error, please contact the sender immediately. Zelis, 2018.

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo