I'm relatively new to creating maps in R using ggplot2, but I have been struggling with this problem for several days. I created my story, but I canβt increase the number of boxes used to match colors with my values.
This is a problem since the card does not do a good job of showing data changes. I am not sure if I approached this issue correctly.
Here is my code:
region=c('alaska','alabama','arkansas','arizona','california','colorado','connecticut','florida','georgia','hawaii','iowa','idaho','illinois','indiana','kansas','kentucky','louisiana','massachusetts','maryland','maine','michigan','minnesota','missouri','mississippi','montana','north carolina','north dakota','nebraska','new hampshire','new jersey','new mexico','nevada','new york','ohio','oregon','pennsylvania','south carolina','south dakota','tennessee','texas','utah','virginia','vermont','washington','wisconsin','west virginia','oklahoma','wyoming') sales=c(46,1240,471,2292,13427,1574,261,10036,826,1508,184,939,2356,1329,434,271,714,208,2027,21,950,500,1871,147,249,1204,69,175,369,1968,606,656,2369,2422,525,2902,1709,126,1563,12046,931,2271,46,2260,250,122,0,0) state_data = as.data.frame(cbind(region,sales)) library(ggplot2) library(maps) all_states <- map_data("state") D = merge(all_states, state_data, by = "region") D = D[with(D,order(D$group,D$order)),] p = ggplot() p = p + geom_polygon( data=D, aes(x=long, y=lat, group = group, fill=D$sales),colour="white" ) p = p + xlab("") p = p + ylab("") p = p + labs(title = "sales") p = p + guides(color=FALSE) p = p + guides(size=FALSE) p = p + guides(fill=guide_legend() ) p = p + guides(fill= guide_colorbar(title="sales",barheight = 1,barwidth=15,direction="horizontal",nbin=8) ) p = p + theme(legend.position="bottom") p
Ideally, I would like to increase the number of cells in the legend to about 8-10 and maybe add another color to the gradient to show additional details. I experimented with ggplot2 functions, but I was not very lucky.