繁体   English   中英

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

[英]Highcharter Map with Categorical Variables in R

我试图在R中使用highcharts包装器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") 

这显然可以在高级图表中使用,但是我似乎无法在高级图表中使用它。

任何输入,不胜感激。

谢谢!

第三系列中没有数据的状态显示为灰色状态,并且位于先前系列中的数据之上。 您可以将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")

您只需要复制所需的示例。

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)

暂无
暂无

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

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