简体   繁体   English

R中具有分类变量的Highcharter映射

[英]Highcharter Map with Categorical Variables in R

I am trying to use the highcharts wrapper highcharter in R to create a series of maps. 我试图在R中使用highcharts包装器highcharter创建一系列地图。 Maps that plot state or country color to a continuous variable work beautifully, however, I'm having some trouble plotting state color to a continuous variable. 将州或国家/地区颜色绘制为连续变量的地图可以很好地工作,但是,在将州/州颜色绘制为连续变量时遇到了一些麻烦。 (Basically, I'd like it to look something like this ). (基本上,我希望它看起来像这样 )。

I've tried everything that I can think of and nothing seems to work. 我已经尝试了所有可以想到的方法,但似乎没有任何效果。 Here's an example with dummy data. 这是虚拟数据的示例。 Assume that I want to show states in category A as red, category B as yellow and category C as blue. 假设我想将类别A中的状态显示为红色,类别B中的状态显示为黄色,类别C中的状态显示为蓝色。

library("dplyr")
library('highcharter')
library("viridisLite") 

data(usgeojson)

## Create data frame with letter categories, numerical categories, and state abbreviations

categories <- c("A", "A", "A", "A", "A", "A", "C", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "C", "A", "A", "A", "B",
"B", "B", "B", "B", "C", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "C", "B", "B", "B", "B" )

states <- c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI",
"ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS",
"MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR",
"PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")

numbers <- c("1", "1", "1", "1", "1", "1", "3", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "3", "1", "1", "1", "2", "2",
"2", "2", "2", "3", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "3", "2", "2", "2", "2" )

data <- data.frame(categories, states, numbers)

## Convert abbreviations to state names for highcharter
data$state_full <- state.name[match(data$state, state.abb)]

## If we plot these data using the numerical categories, the colors are on a scale
highchart(type = "map") %>% 
hc_add_series_map(map = usgeojson, 
                df = data, 
                joinBy = c("woename", "state_full"),
                value = "numbers")

## Plotting by adding each category individually ends up with the each new map
## overwriting the ones before it. 

cat_A <- data[data$categories == "A", ]
cat_B <- data[data$categories == "B", ]
cat_C <- data[data$categories == "C", ]

highchart(type = "map") %>% 
hc_add_series_map(map = usgeojson, 
                df = cat_A, 
                joinBy = c("woename", "state_full"),
                value = "numbers") %>% 
hc_add_series_map(map = usgeojson, 
                df = cat_B, 
                joinBy = c("woename", "state_full"),
                value = "numbers") %>% 
hc_add_series_map(map = usgeojson, 
                df = cat_C, 
                joinBy = c("woename", "state_full"),
                value = "numbers") 

This clearly can work in highcharts but I can't seem to get it to work in highcharter. 这显然可以在高级图表中使用,但是我似乎无法在高级图表中使用它。

Any input is greatly appreciated. 任何输入,不胜感激。

Thank you! 谢谢!

States from third series that doesn't have data are displayed as grey states and are over the data from previous series. 第三系列中没有数据的状态显示为灰色状态,并且位于先前系列中的数据之上。 You could set allAreas to false to prevent this. 您可以将allAreas设置为false来防止这种情况。

Additionally colorAxis needs to get max setting, because it is automatically calculated for the first series only - the issue reported . 另外,colorAxis需要获得最大设置,因为它仅针对第一个系列自动计算- 报告的问题

Working code (to run after your code - after categories are set): 工作代码(在代码之后运行-在设置类别之后):

highchart(type = "map") %>% 
hc_plotOptions(series = list(allAreas = F)) %>%
hc_colorAxis(max = 3) %>%
hc_add_series_map(map = usgeojson, 
                df = cat_A, 
                joinBy = c("woename", "state_full"),
                value = "numbers") %>% 
hc_add_series_map(map = usgeojson, 
                df = cat_B, 
                joinBy = c("woename", "state_full"),
                value = "numbers") %>% 
hc_add_series_map(map = usgeojson, 
                df = cat_C, 
                joinBy = c("woename", "state_full"),
                value = "numbers")

You just need to replicate the example that you want. 您只需要复制所需的示例。

cat_A <- data %>% filter(categories == "A")
cat_B <- data %>% filter(categories == "B")
cat_C <- data %>% filter(categories == "C")

map <- download_map_data("countries/us/us-all")

hc <- highchart(type = "map") %>% 
  hc_plotOptions(map = list(
    allAreas = FALSE,
    joinBy = c("hc-a2", "states"),
    mapData = map
  )) %>% 
  hc_add_series(name = "A", data = cat_A, color = "#A1A1A1") %>% 
  hc_add_series(name = "B", data = cat_B, color = "#46BEC8") %>% 
  hc_add_series(name = "C", data = cat_C, color = "#0000CD")

hc

Other alternative: 其他选择:

series <- data %>% 
  group_by(name = categories) %>% 
  do(data = list_parse(select(., states))) %>%
  ungroup() %>% 
  mutate(color = c("red", "darkred", "pink"))

series

highchart(type = "map") %>% 
  hc_plotOptions(map = list(
    allAreas = FALSE,
    joinBy = c("hc-a2", "states"),
    mapData = map
  )) %>% 
  hc_add_series_list(series)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM