简体   繁体   中英

Highcharter Map with Categorical Variables in R

I am trying to use the highcharts wrapper highcharter in R to create a series of maps. 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.

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.

Additionally colorAxis needs to get max setting, because it is automatically calculated for the first series only - the issue reported .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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