Bill Poling
2018-Sep-13 12:14 UTC
[R] Help with simple Map of US states with predefined regions Version 2
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 at 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}}
Jeff Newmiller
2018-Sep-13 14:28 UTC
[R] Help with simple Map of US states with predefined regions Version 2
Your data appear to be in fixed format, not space-delimited (or delimited by any other special character), so you should use read.fwf to read it in rather that read.table. ?read.fwf In the future you should try to identify where your errors are or your data don't look right and ask focused questions about that (reproducible) problem rather than spilling your whole script into an email. That is, your error occurred in the single read.table command and the rest of it was working fine. On September 13, 2018 5:14:33 AM PDT, Bill Poling <Bill.Poling at zelis.com> wrote:>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 at 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}} > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
Bill Poling
2018-Sep-13 18:31 UTC
[R] Help with simple Map of US states with predefined regions Version 2
Thank you Jeff. Cannot seem to get this to work in the fashion I want it to appear no matter how many websites and packages I investigate. Letting it go for the moment. Always appreciate your advice Sir! WHP From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Sent: Thursday, September 13, 2018 10:29 AM To: r-help at r-project.org; Bill Poling <Bill.Poling at zelis.com>; r-help (r-help at r-project.org) <r-help at r-project.org> Subject: Re: [R] Help with simple Map of US states with predefined regions Version 2 Your data appear to be in fixed format, not space-delimited (or delimited by any other special character), so you should use read.fwf to read it in rather that read.table. ?read.fwf In the future you should try to identify where your errors are or your data don't look right and ask focused questions about that (reproducible) problem rather than spilling your whole script into an email. That is, your error occurred in the single read.table command and the rest of it was working fine. On September 13, 2018 5:14:33 AM PDT, Bill Poling <Bill.Poling at zelis.com<mailto:Bill.Poling at zelis.com>> wrote:>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<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 at 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<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}} > >______________________________________________ >R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html<http://www.R-project.org/posting-guide.html> >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity. 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]]