1

I'm trying to use choroplethr to make a map at the county level. Currently, I have 3 categorical integers (1, 2, 3) in my csv under the column value which vary depending on each county. The region column contains county fips.

I want to display the following values as the respective label , color (value = label = color):

0 = "None" = "white", 1 = "MD" = "#64acbe", 2 = "DO" = "#c85a5a", 3 = "Both" = "#574249",

I've tried several combinations of scale_fill_brewer without the results I'm looking for. Any assistance would be great. Here's code that simulates the data I'm using:

library(choroplethr)
library(ggplot2)
library(choroplethrMaps)

Res <- data.frame(

region = c(45001, 22001, 51001, 16001, 19001, 21001, 29001, 40001, 8001, 19003, 16003, 17001, 18001, 28001, 38001, 31001, 39001, 42001, 53001, 55001, 50001, 72001, 72003, 72005, 72007, 72009, 45003, 27001),

value = c(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3), 
stringsAsFactors = FALSE)

county_choropleth(Res, 
            title = "All United States Medical Residencies",
            legend = "Types of Medical Residencies"
             )
  • 1
    Hi, I am reviewing your post. Although a good question, it is always a good idea to add some code, to show the community your efforts. – rainer May 13 '20 at 12:19
  • Hi Dario. Thank you for using Choroplethr, and thank you for asking your question on Stack Overflow. Following up with what Ranier said. Here are great instructions for how to create a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610. If you edit your existing question to be in this format, it will help the community to answer your question. – Ari May 13 '20 at 15:10
  • Hi all, I will figure out how to include the code I've been running. I appreciate your willingness to help. – Dario Marotta May 13 '20 at 21:12
  • Updated with code, hopefully this helps! Also, let me know if I can clarify anything. – Dario Marotta May 13 '20 at 22:56

1 Answers1

0

Thank you for using Choroplethr.

I think that there are a few issues here. The first one I'd like to address is that your value column contains numeric data. This by itself is not a problem. But because you are actually using it to code categorical data (i.e. "MD", "OD", etc.) this is a problem. Therefore my first task will be to change the data type from numeric to character data:

> class(Res$value)
[1] "numeric"
> Res$value = as.character(Res$value)
> class(Res$value)
[1] "character"

Now I will replace the "numbers" with the category names that you want:

> Res[Res=="0"] = "None"
> Res[Res=="1"] = "MD"
> Res[Res=="2"] = "DO"
> Res[Res=="3"] = "Both"
> head(Res)
  region value
1  45001  None
2  22001    MD
3  51001    DO
4  16001  Both
5  19001  None
6  21001    MD

Now for the second issue. You said that you were trying to use scale_fill_brewer. That function is for using the Brewer scales. But you don't want those here. You say that you have your own scale. So you want to use scale_fill_manual.

county_choropleth(Res) + 
  scale_fill_manual(values=c("None" = "#ffffffff",  
                             "MD" = "#64acbe", 
                             "DO" = "#c85a5a", 
                             "Both" = "#574249"),
                    name="Types of Medical Residencies")

enter image description here

Note: What choroplethr calls the "legend" (which is actually the name of the legend) is actually a property of the ggplot2 scale. In particular, it is the name of the scale. So if you are using your own scale, you cannot use choroplethr's legend parameter any more.

Of course, now we have a new problem: Alaska and Hawaii are all black. I actually forgot about this issue (it's been a while since I worked on Choroplethr). The reason this happens is very technical, and perhaps more detailed than you care for, but I will mention it here for completeness: choroplethr uses ggplot2 annotations to render AK and HI in the proper place. the choropelthr + ggplot_scale paradigm does not work here for AK and HI because ggplot does not propogate additional layers / scales to annotations. To get around this we must use the object-oriented features of choroplethr:

c = CountyChoropleth$new(Res)
c$title = "All United States Medical Residencies"
c$ggplot_scale = scale_fill_manual(values=c("None" = "#ffffffff", "MD" = "#64acbe", "DO" = "#c85a5a", "Both" = "#574249"), 
                                   name="Types of Medical Residencies")
c$render()

enter image description here

Ari
  • 1,819
  • 14
  • 22
  • 1
    This is a great and thorough answer, thank you. One thing - the legend remains "value" even after changing that section of the code. Is there anything that can be used to override this? – Dario Marotta May 14 '20 at 00:54