Assigning colors to table values using Choroplethr

非 Y 不嫁゛ 提交于 2021-01-28 19:44:16

问题


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:


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")

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()



来源:https://stackoverflow.com/questions/61771543/assigning-colors-to-table-values-using-choroplethr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!