简体   繁体   中英

choropleth in ggmap not plotting correctly

I think I just made the world's weirdest choropleth.

The code is 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 for a dput of the dataframe)

But the map is crazy:

在此处输入图片说明

Where did I go wrong?

Looking at other code snippets, I can't seem to figure out how to make this work.

You could plot a choropleth like 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, by = "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, you gotta match the region names in your df$State to the ones in states$region . Then, the map data is merged with your data frame and put in order. Finally, the plot is a piece of cake using ggplot.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM