r - choropleth in ggmap not plotting correctly -
i think made world's weirdest choropleth.
the code simple:
df$cut <- cut_interval(df$count, 5) ggplot(df, aes(lon, lat)) + geom_polygon(aes(fill = df$cut), colour = alpha("white", 1/2), size = 0.2) + geom_polygon(data = df , colour = "white", fill = na) + scale_fill_brewer(palette = "purd")
(see pastebin dput
of dataframe)
but map crazy:
where did go wrong?
looking @ other code snippets, can't seem figure out how make work.
you plot choropleth this:
library(ggplot2) library(xml) tab <- readhtmltable("http://www.50states.com/abbreviations.htm", which=1) df$region <- tolower(tab[match(df$state, tab[, 2]), 1]) states <- map_data("state") choro <- merge(states, df, sort = false, = "region") choro <- choro[order(choro$order), ] ggplot(choro, aes(long, lat.x)) + geom_polygon(aes(group = group, fill = count), colour="grey55") + coord_quickmap() + facet_wrap(~year)
first, gotta match region names in df$state
ones in states$region
. then, map data merged data frame , put in order. finally, plot piece of cake using ggplot.
Comments
Post a Comment