This comes from the ggplot2 documentation:
# Better example
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
library(reshape2) # for melt
crimesm <- melt(crimes, id = 1)
if (require(maps)) {
states_map <- map_data("state")
ggplot(crimes, aes(map_id = state)) + geom_map(aes(fill = Murder), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat)
last_plot() + coord_map()
ggplot(crimesm, aes(map_id = state)) + geom_map(aes(fill = value), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat) + facet_wrap( ~ variable)
}
I don't understand how this can work since there is no common identifier in the states_map data frame which uses "region" to name the states column and the crimes data frame which labels the states, "states." What ties the data to the map?
In this example the poster renames the columns of the map data frame to conform with the data but the ggplot2 documentation doesn't seem to do it. How? When I rename the columns of states_map in the example above, so that "state" is common to both data frames, it breaks the code.
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
library(reshape2) # for melt
crimesm <- melt(crimes, id = 1)
if (require(maps)) {
states_map <- map_data("state")
##### my attempt to fix what isn't broken #################
names(states_map)[5]<-"state"
###########################################################
ggplot(crimes, aes(map_id = state)) + geom_map(aes(fill = Murder), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat)
last_plot() + coord_map()
# Error: all(c("x", "y", "id") %in% names(map)) is not TRUE
}
Thanks.