Hello,
My goal is to draw a US map similar to the example provided in Winston Chang's R Graphic Cookbook (pages 317-318) except that I wanted to add a title and add Alaska and Hawaii to the map.
The reproducible example is appended below .
There are 2 issues:
1) I got the following error message after the theme_clean() function:
Error in seq_len(nrow(data) - 1) :
argument must be coercible to non-negative integer
2) I could not figure out how to add Alaska and Hawaii to the map.
I would appreciate if someone could help me resolve the issuea.
Thanks in advance.
Pradip Muhuri
The reproducible example begins here
#################
setwd ("E:/CSAP_P1")
library(maps)
library(plyr)
loadedNamespaces()
#read estimates of risk factors for substances use (ages 12-17) by State obtained from SUDAAN output
p1_st_data <-read.table (text="
Alabama,49.60, 1.37
Alaska,55.00, 1.41
Arizona,52.50, 1.56
Arkansas,50.50, 1.22
California, 51.10, 0.65
Colorado,55.10, 1.26
Connecticut,56.30, 1.28
Delaware, 53.60, 1.30
District of Columbia, 53.50, 1.22
Florida, 52.70, 0.67
Georgia, 52.50, 1.15
Hawaii, 49.40, 1.33
Idaho, 48.30, 1.23
Illinois, 52.70, 0.63
Indiana, 49.60, 1.16
Iowa, 46.30, 1.37
Kansas, 44.30, 1.43
Kentucky, 52.90, 1.37
Louisiana, 49.70, 1.23
Maine, 55.60, 1.44
Maryland, 53.90, 1.46
Massachusetts, 55.40, 1.41
Michigan, 52.40, 0.62
Minnesota, 51.50, 1.20
Mississippi, 43.20, 1.14
Missouri, 48.70, 1.20
Montana, 56.40, 1.16
Nebraska, 45.70, 1.51
Nevada, 54.20, 1.17
New Hampshire, 56.10, 1.30
New Jersey, 53.20, 1.45
New Mexico, 57.60, 1.34
New York, 53.70, 0.67
North Carolina, 52.20, 1.26
North Dakota, 48.60, 1.34
Ohio, 50.90, 0.61
Oklahoma, 47.20, 1.42
Oregon, 54.00, 1.35
Pennsylvania, 53.00, 0.63
Rhode Island, 57.20, 1.20
South Carolina, 50.50, 1.21
South Dakota, 43.40, 1.30
Tennessee, 48.90, 1.35
Texas, 48.70, 0.62
Utah, 42.00, 1.49
Vermont, 58.70, 1.24
Virginia, 51.80, 1.18
Washington, 53.50, 1.39
West Virginia, 52.80, 1.07
Wisconsin, 49.90, 1.50
Wyoming, 49.20, 1.29",
sep= "," , col.names = c("state" , "Obt_mrj_p" , "Obt_mrj_se" ),
colClasses = c( "character" , "numeric" , "numeric" )
)
#create a new variable, region
names(p1_st_data) <- tolower (names(p1_st_data))
p1_st_data$region <- tolower(p1_st_data$state)
p1_st_data$ob_mrj_cat <- cut (p1_st_data$obt_mrj_p, quantile (p1_st_data$obt_mrj_p, (0:5/5)),
include.lowest=TRUE)
#load maps data library
library(maps)
states_map <- map_data("state")
head (states_map)
#merge US map data with NSDUH estimates by region (state)
drug_map <- merge (states_map, p1_st_data,
by.states_map = "region", by.p1_st_data = "state")
head (drug_map)
#sort by group, then order
drug_map <- arrange (drug_map, group, order)
theme_clean <- function (base_size=12) {
require(gridBase)
theme_grey(base_size) %+replace%
theme(
axis.title = element_blank (),
axis.text = element_blank (),
panel.background = element_blank (),
panel.grid = element_blank (),
axis.ticks.length = unit (0,"cm"),
axis.ticks.margin = unit (0,"cm"),
panel.margin = unit (0,"lines"),
plot.margin = unit(c(0,0,0,0),"lines"),
complete =TRUE
)
}
#generate duscrete color palete with 5 values
pal <- colorRampPalette(c("#559999", "grey80", "#BB650B")) (5)
# US map on % of those who said - easy to get marijuana (Obt_mrj_p)
ggplot(drug_map, aes(map_id=state, fill = ob_mrj_cat)) +
geom_map (map=states_map, colour="black")+
scale_fill_manual (values=pal)+
expand_limits (x=states_map$long, y=states_map$lat)+
coord_map ("polyconic")+
labs (fill= "Pecentages")+
ggtitle ("Fairly Easy/Very Easy to Obtain Marijuana
\nas Reported by Youths (Ages 12 to 17)
\nPercentages, Annual Averages 2002-2008 NSDUH ")+
theme_clean()
ggsave (file="US_map_mrj_rev.png", width=11, height=7)