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

Bill Poling Bill@Poling @ending from zeli@@com
Thu Sep 13 14:17:26 CEST 2018


Hi,

I hope someone can help me finalize this please.

I am coming close to what I need using variations from two ggplot2 tutorials.

This first gives me the map of the US with AK & HI but I cannot figure out how to get my 5 regions colored

#https://stackoverflow.com/questions/38021188/how-to-draw-u-s-state-map-with-hi-and-ak-with-state-abbreviations-centered-us?rq=1


library(ggplot2)
install.packages("ggalt")
library(ggalt)     # coord_proj
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
install.packages("ggthemes")
library(ggthemes)  # theme_map
install.packages("rgeos")
library(rgeos)     # centroids
library(dplyr)

# composite map with AK & HI
usa_map <- usa_composite()

# calculate the centroids for each state gCentroid(usa_map, byid=TRUE) %>%
  as.data.frame() %>%
  mutate(state=usa_map using data$iso_3166_2) -> centroids

# make it usable in ggplot2
usa_map <- fortify(usa_map)

View(usa_map)
t1 <- head(usa_map,n=5)
knitr::kable(t1, row.names=FALSE, align=c("l", "l", "r", "r", "r"))

#

  # |long      |lat      | group| order|  region|subregion |
  # |:---------|:--------|-----:|-----:|-------:|:---------|
  # |-87.46201 |30.38968 |     1|     1| alabama|NA        |
  # |-87.48493 |30.37249 |     1|     2| alabama|NA        |
  # |-87.52503 |30.37249 |     1|     3| alabama|NA        |
  # |-87.53076 |30.33239 |     1|     4| alabama|NA        |
  # |-87.57087 |30.32665 |     1|     5| alabama|NA        |

usa_map <- fortify(usa_map)
gg <- ggplot()
gg <- gg + geom_map(data=usa_map, map=usa_map,
                    aes(long, lat, map_id=id),
                    color="#2b2b2b", size=0.1, fill=NA)

gg <- gg + geom_text(data=centroids, aes(x, y, label=state), size=2) gg <- gg + coord_proj(us_laea_proj) gg <- gg + theme_map() gg




#************************************************************************************************************************************************************************************/

This second is an alternative (however not liking AK&HI, not coming into the map like scenario one above) but also ignoring new Mexico (because recognizing a seventh field value) and I suspect it will do the same for new York and new jersey etc.. when I add them to the list.

Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  :  line 12 did not have 6 elements

When I use newmexico (all one word) it appears white in the map like the other states not in the table statement

#https://stackoverflow.com/questions/38777732/r-code-to-generating-map-of-us-states-with-specific-colors

library(ggplot2)

read.table(text="State.Code   region            St_Abbr   Num_Estab  colors
                      1          1   alaska       Ak        13123    #f7931e
                      3          1   arizona      AZ        18053    #f7931e
                      5          1   california   CA       143937    #f7931e
                      2          1   hawaii       HI       123456    #f7931e
                      4          1   nevada       NV       654321    #f7931e
                      6          1   oregon       OR       321456    #f7931e
                      7          1   washington   WA       456123    #f7931e
                      8          2   colorado     CO       987654    #787878
                      9          2   idaho        ID       13549     #787878
                     10          2   kansas       KS       94531     #787878
                     11          2   montana      MT       456321    #787878
                     12          2   new mexico   NM     582310            #787878 <---Not liking new mexico, saying not 6
                     13          2   oklahoma     OK       214567    #787878
                     14          2   texas        TX       675421    #787878
                     15          2   utah         UT       754321    #787878
                     16          2   wyoming      WY       543124    #787878 ",
stringsAsFactors=FALSE, header=TRUE, comment.char="") -> df

usa_map1 <- map_data("state")
t1 <- head(usa_map1,n=5)
knitr::kable(t1, row.names=FALSE, align=c("l", "l", "r", "r", "r"))
View(usa_map1)
#
#   |long      |lat      | group| order|  region|subregion |
#   |:---------|:--------|-----:|-----:|-------:|:---------|
#   |-87.46201 |30.38968 |     1|     1| alabama|NA        |
#   |-87.48493 |30.37249 |     1|     2| alabama|NA        |
#   |-87.52503 |30.37249 |     1|     3| alabama|NA        |
#   |-87.53076 |30.33239 |     1|     4| alabama|NA        |
#   |-87.57087 |30.32665 |     1|     5| alabama|NA        |



gg <- ggplot()
#View(gg)
gg <- gg + geom_map(data=usa_map1, map=usa_map1,
                    aes(long, lat, map_id=region),
                    color="#2b2b2b", size=0.15, fill=NA)

gg <- gg + geom_map(data=df, map=usa_map1,
                    aes(fill=colors, map_id=region),
                    color="#2b2b2b", size=0.15)


gg <- gg + geom_text(data=centroids, aes(x, y, label=state), size=2) gg <- gg + coord_proj(us_laea_proj) gg <- gg + theme_map() gg


gg <- gg + scale_color_identity()
gg <- gg + coord_map("polyconic")
gg <- gg + ggthemes::theme_map()
gg

#c( "colorado", "idaho", "kansas", "montana", "new mexico", "oklahoma","texas", "utah", "wyoming") ) #c("alaska", "arizona", "california", "hawaii", "nevada", "oregon","washington"))



William H. Poling, Ph.D., MPH




Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}}



More information about the R-sig-Geo mailing list