R中用Highcharter制作分类变量地图

3

我正在尝试使用R中的高级图表包highcharter创建一系列地图。将州或国家颜色绘制为连续变量的地图效果非常好,但是,使用连续变量绘制州颜色时遇到了一些问题(基本上,我想让它看起来像这样)。

我已经尝试了所有能想到的方法,但似乎没有什么作用。这里有一个带虚拟数据的示例。假设我想要显示类别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") 

这显然可以在Highcharts中使用,但似乎无法在Highcharter中使用。

非常感谢任何建议。

谢谢!


有趣-无关紧要,但当我运行它时,现在出现错误,提示“找不到对象'usgeojson'”。已测试我的其他highcharter地图类型项目,它们也失败了。啊,是我打错了。现在地图可以加载了。但是,是的,它们仍然是单色的。 - wergeld
哦!对不起!我编辑了脚本并添加了必要的 data(usgeojson) 调用。 - Amber Thomas
你好!你找到解决问题的方法了吗?我也遇到了同样的问题。谢谢! - Bruno Guarita
2个回答

1

第三个系列中没有数据的状态将显示为灰色状态,并覆盖前面系列的数据。您可以将allAreas设置为false以防止此情况发生。

另外,colorAxis需要获取最大值设置,因为它仅自动计算第一个系列 - 已报告的问题

工作代码(在设置类别后运行):

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

1
你只需要复制想要的示例即可。
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

其他替代方案:
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接