Custom Bins in Choroplethr

407 views Asked by At

I am using choroplethr to produce choropleths for the rates of a certain health outcome by US state (at the census tract level). After wrestling with the program I was able to get it running, but I would like to modify the bins. I found this link about how to manually code breaks: (https://www.r-bloggers.com/advanced-choroplethr-customized-bins/)

I manually coded my bins but So I coded my cutpoints and the map that was generated looks like:

Generated map

My lowest bins got put together! Is there a way to specify that I don’t want choroplethr to do that? Is the “num_colors” command my best choice?

Thanks! Sam

ETA: I'm not sure I can make a reproducible example of this. Someone would need to have the FL census tract FIPS codes handy to merge with the values that I would generate using a random number generator. But here is my code in case this helps. I was hoping someone would be able to spot if there was a way to add a specific line of code to stop bins from automatically collapsing.

#manually label classes by given cut points
mydata2$countclass[mydata2$rate_acs ==0 ]<-0
mydata2$countclass[mydata2$rate_acs >0 & mydata2$rate_acs <32.3 ]<-1
mydata2$countclass[mydata2$rate_acs >32.3 & mydata2$rate_acs <43.2 ]<-2
mydata2$countclass[mydata2$rate_acs >43.2 & mydata2$rate_acs <55.8 ]<-3
mydata2$countclass[mydata2$rate_acs >55.8 & mydata2$rate_acs <74.3 ]<-4
mydata2$countclass[mydata2$rate_acs >74.3 ]<-5
mydata2$value = mydata2$countclass
tract_choropleth(mydata2, "florida")
1

There are 1 answers

1
SmilingSam On BEST ANSWER

It's been days of research and I've finally come up with a solution.

I followed the approach on this question: Is there a way that I can manually control the thresholds between various range on choroplethr?

(Instead of using "num_colors" which didn't work for me) someone advised to use a cut function to create breaks. My end code (that worked) looked like:

# manually label classes by given cut points
# define the breaks you want
mydata2$value <- cut (mydata2$rate_dec, breaks = c(0, 32.3, 43.2, 55.8, 74.3, 1000))
tract_choropleth(mydata2, "florida")

My code assigns a cut function from the "old" column named "rate_dec" to a new column called "value" (which is needed for choropleth to run). When I rerun the map using the value column with the cuts then I get a map with my desired bins.

https://i.stack.imgur.com/th5ve.jpg

PS: Don't mind the value of "1000"--I'm working with some skewed data