#More complex examples with formatting are shown in the map_examples demo for the package
#create synthetic survey dataset
state_codes <- c('AL','AK','AZ','AR','CA','CO','CT','DE','DC','FL','GA','HI','ID','IL',
'IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE',
'NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD',
'TN','TX','UT','VT','VA','WA','WV','WI','WY')
surveydata <- data.frame(state=factor(rep(rep(state_codes,
times=3), times=2)))
surveydata$year <- rep(c(2009, 2010), each=nrow(surveydata)/2)
surveydata$obs_weight <- runif(n=nrow(surveydata), min=0.8, max=1.5)
#two income distributions
surveydata$income <- 100000*rbeta(n=nrow(surveydata),
shape1=ifelse(surveydata$year==2009, 2, 1.5),
shape2=ifelse(surveydata$year==2010, 10, 11))
surveydata$income_ge20k <- ifelse(surveydata$income >=20000, 100, 0)
#these state and year combinations will not be shaded if they are missing entirely
surveydata[ surveydata$state == "NV" & surveydata$year == 2009, c("income","income_ge20k")] <- NA
surveydata[ surveydata$state == "OH" & surveydata$year == 2010, c("income","income_ge20k")] <- NA
#load map shapefile
usMap <- readShapePoly(system.file("shapes/usMap.shp", package="mapStats")[1])
#Calculate weighted mean of variable income by state. Display using red
#sequential color palette with 4 groups. In the titles, rename 'income'
#by 'household income'.
mapStats(d=surveydata, var="income", wt.var="obs_weight",
map.file=usMap, x.geo.var="state", map.geo.var="STATE_ABBR",
stat=c("mean"), ngroups=4, palette="Reds",
var.pretty="household income", geo.pretty="state",
map.label=TRUE)
#Calculate the weighted mean and 40th and 50th quantiles of the variable income
#by state and survey year. Display 2 statistics on the first page and 1 on the
#last, and use three color palettes
mapStats(d=surveydata, var="income", by.var="year",
wt.var="obs_weight", map.file=usMap,
x.geo.var="state", map.geo.var="STATE_ABBR",
stat=c("mean","quantile"), quantiles=c(.4, .5),
ngroups=6, palette=c("Reds","Greens","Blues"),
var.pretty="household income", geo.pretty="state",
by.pretty="Year", map.label=TRUE, num.col=1, num.row=2,
layout=c(2,1))
#To calculate percentages of class variables, create an indicator variable, calculate
#its mean, and override the default titles to say you are calculating the percentage.
#Here we illustrate by calculating the percent of respondents by state that have income
#above $20,000.
mapStats(d=surveydata, var="income_ge20k", wt.var="obs_weight",
map.file=usMap, x.geo.var="state", map.geo.var="STATE_ABBR",
col.pal="Reds", stat=c("mean"),
titles="Percent of respondents with income at least $20,000")
#calculating statistics using the functions outside of mapStats
#unweighted quantiles
calcQuantiles(d=surveydata, x.geo.var="state", var="income",
by.var="year", quantiles=c(0.5, 0.75))Run the code above in your browser using DataLab