US maps - qplot and its translation to ggplot

225 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Dec 31, 2012, 11:38:48 PM12/31/12
to ggp...@googlegroups.com, pradip...@samhsa.hhs.gov
Hello,

Using the code and data (reproducible example) shown below, I have drawn two maps - qplot and ggplot. 
Acknowledgement: Hadley Wicham for the qplot example in ggplot2 (pages 78-79).

The issues are as follows:

1) Alaska and Hawaii are not displayed in the map. I am not sure whether the longitude and latitude data for these two States are extracted.
2) There appear to be some problems in the map created by the ggplot () - some tweaking of the code is needed.
3) I could not figure out how to add the State code to the map in qplot as well as ggplot.

I would appreciate receiving any advice/help toward resolving the issues.

Happy New Year!

Thanks and regards,

Pradip Muhuri

###############  reproducible example shown below  ###############


#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)


p1_st_data$ob_mrj_cat
p1_st_data []


#load ggplot2 library
library(ggplot2)

#load maps data library
library(maps)

#state-level longitude and lattitude data
ori_states <- map_data("state") # can't find "alaska" and "hawaii"

dim (ori_states) # why so many observations?

identical(ori_states$region, p1_st_data$region) # why FALSE?

#merge US map data with NSDUH estimates by region (state)
m_states <- merge (ori_states, p1_st_data,
                   by.ori_states = "region", by.p1_st_data = "state"); dim (m_states)

#reorder the rows
m_states <- m_states [order(m_states$order),];

dim(m_states)

#print tail observations
tail (m_states)

# US map on % who said - easy to get marijuana (Obt_mrj_p) from the merged data called m_states
# using the example in Hadley's ggplot2 - pages 78-79
qplot(long, lat, data=m_states, group=group, fill=ob_mrj_cat, geom="polygon")

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

#translate the above qplot into ggplot -  The code below does not seem to work
p <- ggplot()
p <- p + geom_polygon( data=m_states, aes(x=long, y=lat, group = group),
                       colour="white", fill="grey60")

p <- p + geom_polygon ( data=m_states, aes(x=long, y=lat, fill = ob_mrj_cat))  +
                       #scale_colour_manual(values=cbPalette)
                       scale_fill_manual (values=cbPalette)
p

pradip...@samhsa.hhs.gov

unread,
Jan 4, 2013, 2:09:35 PM1/4/13
to ggp...@googlegroups.com, pradip...@samhsa.hhs.gov
Hello,
 
Please accepting my apology for reposting this (originally posted 12/31/2012).
 
My goal is to draw a qplot-equivalent  US map in ggplot, with the addition of Alaska and  Hawaii.  
 
Issues:
 
1) Using the example in Hadley's ggplot2 - pages 78-79, the following code has worked for me although Alaska and Hawaii are not displayed in the map. 

qplot(long, lat, data=m_states, group=group, fill=ob_mrj_cat, geom="polygon")
 
2) The following ggplot 2 code has given me the map that does not seem to  have problems.
 
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
p <- ggplot()

p <- p + geom_polygon ( data=m_states, aes(x=long, y=lat, fill = ob_mrj_cat))  +
                       #scale_colour_manual(values=cbPalette)
                       scale_fill_manual (values=cbPalette)
p
 
3) Alaska and Hawaii are not displayed.  Is this because Alaska and Hawai map data are not included in the map_data?
I would appreciate receiving any advice/help toward resolving the issues.

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 4, 2013, 2:13:10 PM1/4/13
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com

Hello,

 

Oops!  The earlier e-mail with typos was sent inadvertently. Sorry.

 

Please accept my apology for reposting this (originally posted 12/31/2012).

Brandon Hurr

unread,
Jan 7, 2013, 5:32:05 AM1/7/13
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
You're right that the "states" dataset does not include Alaska and Hawaii. There are a few StackOverflow questions that came up with a quick google search, but it seems like getting Alaska and Hawaii in the same picture requires significant hacking. 




--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 7, 2013, 6:18:07 AM1/7/13
to Brandon Hurr, ggp...@googlegroups.com
Hi Brandon,

Thanks for your response.  Please see Number (2) below.   regards,  Pradip


1) No issues :  #US map on % who said - easy to get marijuana (Obt_mrj_p) from the merged data called  #m_states using the example in Hadley's ggplot2 - pages 78-79

qplot(long, lat, data=m_states, group=group, fill=ob_mrj_cat, geom="polygon")

#2)  Issues:  Does the following ggplot syntax adequately translate the preceding # qplot syntax in (1)?
# reproducible example below (data)


cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

#translate the above qplot into ggplot -  The code below does not seem to work
p <- ggplot()
p <- p + geom_polygon ( data=m_states, aes(x=long, y=lat, fill = ob_mrj_cat))  +
                       #scale_colour_manual(values=cbPalette)
                       scale_fill_manual (values=cbPalette)
p

From: Brandon Hurr [brando...@gmail.com]
Sent: Monday, January 07, 2013 5:32 AM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggp...@googlegroups.com
Subject: Re: US maps - qplot and its translation to ggplot

Brandon Hurr

unread,
Jan 7, 2013, 6:29:16 AM1/7/13
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
It does if you add, group...

ggplot(data=m_states, aes(x=long, y=lat, fill = ob_mrj_cat, group=group)) + geom_polygon ()

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 8, 2013, 4:31:03 PM1/8/13
to Brandon Hurr, ggp...@googlegroups.com

Dear Brandon,

 

Thank you so much for your help with code revisions.  The R/ggplot2 script and the map are attached. Any thoughts on adding boundary to the map? I could not find

 

Regards,

 

Pradip

 

 

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 2-1071

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

xP1_maps2.R
US_map_mrj_a.png

Brandon Hurr

unread,
Jan 9, 2013, 7:57:20 AM1/9/13
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
borders(database="state", colour="black")

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 9, 2013, 10:00:43 AM1/9/13
to Brandon Hurr, ggp...@googlegroups.com

Dear Brandon,

 

Thank you so much for your help with my revision of the map.  Attached is the R script and the output (without Alaska and Hawaii).  

US_map_mrj_a.png
xP1_maps2.R

Brandon Hurr

unread,
Jan 9, 2013, 10:04:24 AM1/9/13
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
Aesthetically speaking, consider getting rid of the background, axes, axes labels and grid. 

David Kahle

unread,
Jan 9, 2013, 10:11:38 AM1/9/13
to Brandon Hurr, Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
And coord_map.

david.
Reply all
Reply to author
Forward
0 new messages