Learn R Programming

mosaic (version 0.13.0)

mUSMap: Make a US map with ggplot2

Description

mUSMap takes in one dataframe that includes information about different US states. It merges this dataframe with a dataframe that includes geographical coordinate information. Depending on the arguments passed, it returns this data or a ggplot object constructed with the data.

Usage

mUSMap(data, key, fill = NULL, plot = c("borders", "frame", "none"),
  style = c("compact", "real"))

Arguments

data
A dataframe with US states as cases
key
The column name in the data that holds the unique names of each state
fill
A variable in the data used to specify the fill color of states in the map (note: if fill is not null, then plot cannot be set to "none")
plot
The plot desired for the output. plot = "none" returns the merged data that is the result of merging the data and the dataframe with the geographical coordinate information; plot = "frame" returns an empty (unplottab
style
The style in which to display the map. compact gives a polyconic projection with Alaska and Hawaii on the lower left corner; real gives the real size and position of all states without any projection.

Examples

Run this code
sAnscombe <- Anscombe %>% 
  group_by(state = rownames(Anscombe)) %>% 
  summarise(income = sum(income)) %>%
  mutate(state = standardName(state, c(IO = "IA", KA = "KS"), quiet=TRUE))

mUSMap(sAnscombe, key="state", fill="income")

mUSMap(sAnscombe, key="state", plot="frame") +
geom_point()

mergedData <- mUSMap(sAnscombe, key="state", plot="none")

ggplot(mergedData, aes(x=long, y=lat, group=group, order=order)) +
geom_polygon(aes(fill=state), color="darkgray") + guides(fill=FALSE)

Run the code above in your browser using DataLab