ggplot2 - Drawing colored US State map with cut_number() in R -
i have dataframe called "drawdata":
geoname ranking 1 alabama 15 2 alaska 2 3 arizona 28 4 arkansas 12 5 california 19 6 colorado 7 7 connecticut 42 8 delaware 37 9 district of columbia 9 10 florida 38 11 georgia 11 12 hawaii 48 13 idaho 10 14 illinois 16 15 indiana 26 16 iowa 34 17 kansas 27 18 kentucky 20 19 louisiana 4 20 maine 51 21 maryland 30 22 massachusetts 39 23 michigan 14 24 minnesota 23 25 mississippi 41 26 missouri 32 27 montana 25 28 nebraska 21 29 nevada 45 30 new hampshire 47 31 new jersey 33 32 new mexico 5 33 new york 44 34 north carolina 13 35 north dakota 31 36 ohio 35 37 oklahoma 6 38 oregon 18 39 pennsylvania 40 40 rhode island 49 41 south carolina 29 42 south dakota 46 43 tennessee 43 44 texas 3 45 utah 17 46 vermont 50 47 virginia 8 48 washington 24 49 west virginia 22 50 wisconsin 36 51 wyoming 1
and want draw state map different colors each ranking. code have is:
names(drawdata) = c('region','value') drawdata[,1] = tolower(drawdata[,1]) states = data.frame(state.center, state.abb) states_map = map_data("state") df = merge(drawdata, states_map, = "region") df$num = 49 p1 = ggplot(data = df, aes(x = long, y = lat, group = group)) p1 = p1 + geom_polygon(aes(fill = cut_number(value, num[1]))) p1 = p1 + geom_path(colour = 'gray', linestyle = 2) p1 = p1 + scale_fill_brewer('', palette = 'purd') p1 = p1 + coord_map() p1 = p1 + scale_x_continuous(breaks=null) + scale_y_continuous(breaks=null) p1 = p1 + theme(legend.position="none") p1 = p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = null), size = 2) p1
this works if 'num', or number of colors fill, small. however, when set 'num=49', produces error:
error in cut.default(x, breaks(x, "n", n), include.lowest = true, ...) : 'breaks' not unique
when alter code from
p1 = p1 + geom_polygon(aes(fill = cut_number(value, num[1])))
to
p1 = p1 + geom_polygon(aes(fill = cut_number(unique(value), num[1])))
then gives me different error:
error: aesthetics must either length one, or same length dataproblems:cut_number(unique(value), num[1])
i want map every 49 states in map have different colors, each reflecting 'ranking'. appreciated!
brewer palettes deliberately have small maximums (generally < 12) since it's pretty impossible humans map subtle differences discrete values have. can achieve you're looking "faking" scale_fill_gradient2
(note: deliberately left legend in should too):
library(ggplot2) names(drawdata) <- c('region','value') drawdata[,1] <- tolower(drawdata[,1]) states <- data.frame(state.center, state.abb) states <- states[!(states$state.abb %in% c("ak", "hi")),] # aren't part of states_map states_map <- map_data("state") p1 <- ggplot() # borders p1 <- p1 + geom_map(data=states_map, map=states_map, aes(x=long, y=lat, map_id=region), color="white", size=0.15) # fills p1 <- p1 + geom_map(data=drawdata, map=states_map, aes(fill=value, map_id=region), color="white", size=0.15) # labels p1 <- p1 + geom_text(data=states, aes(x=x, y=y, label=state.abb, group=null), size=2) # decent projection p1 <- p1 + coord_map("albers", lat0=39, lat1=45) p1 <- p1 + scale_fill_gradient2(low="#f7f4f9", mid="#df65b0", high="#67001f") # better theme p1 <- p1 + labs(x=null, y=null) p1 <- p1 + theme_bw() p1 <- p1 + theme(panel.grid=element_blank()) p1 <- p1 + theme(panel.border=element_blank()) p1 <- p1 + theme(axis.ticks=element_blank()) p1 <- p1 + theme(axis.text=element_blank()) p1
you can better result scale_fill_distiller
alot under scenes let use color brewer palette continuous data (i'd argue not have continuous data tho):
p1 <- p1 + scale_fill_distiller(palette="purd")
i'd suggest continuing use cut
had , having max of 9 breaks fit color brewer palette you're trying work with. in reality, folks still going need table grok rankings (never assume americans know either state shapes, locations or two-letter abbreviations them), i'd pretty suggest using actual table full names @ least choropleth if not in place of it.
note way you're trying build map deliberately excluded alaska, hawaii , district of columbia. you'll need use real shapefile , cover here them show nicely.
Comments
Post a Comment