Good morning Don, I cannot thank you enough for your support and the trouble 
you went to.
I am novice useR and the only analyst in the shop asked to learn R and the 
demands are growing faster than my knowledge intake, lots of laughs!

Best regards

WHP

From: MacQueen, Don <macque...@llnl.gov>
Sent: Thursday, September 13, 2018 4:41 PM
To: Bill Poling <bill.pol...@zelis.com>; r-sig-...@r-project.org
Subject: Re: [R-sig-Geo] Help with simple Map of US states with predefined 
regions Version 2

I know this is not a complete solution -- and it's a very different approach -- 
but it should at least show you a way to reliably get states colored by region.
(I also left out Alaska and Hawaii, since the point here is how to color the 
regions)

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<https://www.census.gov/cgi-bin/geo/shapefiles/index.php?year=2017&layergroup=States+%28and+equivalent%29>

## unzip to working directory ( '.' )
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<http://state.name> comes 
with R)
cus <- subset(ustf, NAME %in% state.name<http://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)

(my preferred graphics device does not support Polypath, but probably most 
others do, so one can omit usePolypath=FALSE)

-Don

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



On 9/13/18, 5:17 AM, "R-sig-Geo on behalf of Bill Poling" 
<r-sig-geo-boun...@r-project.org on behalf of 
bill.pol...@zelis.com<mailto:r-sig-geo-boun...@r-project.org%20on%20behalf%20of%20bill.pol...@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@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-sig-Geo mailing list
r-sig-...@r-project.org<mailto:r-sig-...@r-project.org>
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-help@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.

Reply via email to