choroplethrZip
choroplethrZip copied to clipboard
Update zip_choropleth.R
This change adds an optional "palette" variable to the zip_choropleth function so that the user has the ability to set the color palette for the Alaska and Hawaii insets. Currently if you try to set the color via scale_fill_brewer it does not update the Alaska and Hawaii colors. Example below, using data(df_pop_zip): df_pop_zip %>% zip_choropleth2(num_colors = 8,title = "Testing") + scale_fill_brewer(palette="RdYlGn", drop=FALSE)
Change will allow you to do the following: df_pop_zip %>% zip_choropleth2(num_colors = 8,title = "Testing",palette = "RdYlGn")
Open for other suggestions but I am needing to apply a custom color palette.
Great find, Heather. The current package's blue-coloring of Hawaii and Alaska looks quite odd versus the red/greens for the continental U.S. You have inspired me to figure out how to integrate my subtitle argument, which hopefully will not conflict with your work.
After further research I found I could change the HI/AK colors using the R6 class with render_nationwide(). With little data in these regions I didn't like how HI/AK are floating. I've instead added at option inset_outline to the ChoroplethZip class and zip_choropleth function. The USA choro map defaults to black outlines but I didn't want to change anyone's maps if this change gets merged. If you specify the outline color it will be incorported into the render_nationwide() function for HI/AK, you still need to specify outlines for the continent as you would today. Below I've attached a set of 5 examples walking through the updated function still working as it would today and how the new code allows the outlines to be added. I also demo how to use the R6 class with this change to change colors and outlines of the entire map.
#Showing no change - leaving HI and AK without outlines df_pop_zip %>% zip_choropleth(num_colors = 8,title = "Heather Test") + geom_polygon(data=us,aes(x=long,y=lat,group=group),color="gray",fill=NA,alpha=.35)
#Adding an outline for HI and AK df_pop_zip %>% zip_choropleth(num_colors = 8,title = "Heather Test", inset_outline = "gray") + geom_polygon(data=us,aes(x=long,y=lat,group=group),color="gray",fill=NA,alpha=.35)
#Changing the color of the state fill, doesn't work for HI and AK, keep outline of HI and AK df_pop_zip %>% zip_choropleth(num_colors = 8,title = "Heather Test", inset_outline = "gray") + scale_fill_manual(name = "Testing",values=colorRampPalette(securian_palette_2)(9), drop=FALSE) + geom_polygon(data=us,aes(x=long,y=lat,group=group),color="gray",fill=NA,alpha=.35)
#Change the colors for HI and AK via the R6 class, defaults to have no outlines choro1<-ZipChoropleth$new(df_pop_zip) choro1$title <- "Heather Test" choro1$set_num_colors(9) choro1$ggplot_scale <- scale_fill_manual(name = "Testing",values=colorRampPalette(securian_palette_2)(9), drop=FALSE) choro1$render_nationwide() + scale_fill_manual(name = "Testing",values=colorRampPalette(securian_palette_2)(9), drop=FALSE)
us<-map_data('state')
#Change the colors for HI and AK via the R6 class, add outlines for HI and AK, add outlines for continental states choro1<-ZipChoropleth$new(df_pop_zip) choro1$title <- "Heather Test" choro1$inset_outline <- "gray" choro1$ggplot_scale <- scale_fill_manual(name = "Testing",values=colorRampPalette(c("#EFAC14","#006699"))(9), drop=FALSE) choro1$render_nationwide() + scale_fill_manual(name = "Testing",values=colorRampPalette(c("#EFAC14","#006699"))(9), drop=FALSE) + geom_polygon(data=us,aes(x=long,y=lat,group=group),color="gray",fill=NA,alpha=.35) #adds outline for continental states