Highcharter中hc_add_series_map合并区域及显示标签技术问询
Great question! Yes, hc_add_series_map is absolutely the right tool for your needs in R's highcharter package. To merge regions, hide internal borders, show group names, and add bold borders around your 5 large regions, we'll adjust your workflow to first aggregate the map and data, then configure the highchart settings properly. Here's a step-by-step solution tailored to your code:
Step 1: Define Your Region Groupings
First, add a grouping column to your map data to assign small regions to each of your 5 large areas. Replace the example French department codes with your actual target regions:
library(highcharter) library(dplyr) library(sf) # For merging map geometries # Load original map data mapdata <- get_data_from_map(download_map_data("countries/fr/fr-all-all")) # Assign regions to 5 custom groups (customize these to fit your needs) mapdata <- mapdata %>% mutate(region_group = case_when( `hc-a2` %in% c("75", "92", "93", "94") ~ "Île-de-France", `hc-a2` %in% c("13", "83", "84") ~ "Provence-Alpes-Côte d'Azur", `hc-a2` %in% c("69", "38", "42") ~ "Auvergne-Rhône-Alpes", `hc-a2` %in% c("59", "62", "02") ~ "Hauts-de-France", TRUE ~ "Grand Est" # Your 5th custom region ))
Step 2: Aggregate Map Geometries and Data
Merge the small region polygons into single shapes for each large group, and aggregate your fake data to match these groups:
# Merge small region geometries into large group polygons aggregated_map <- mapdata %>% group_by(region_group) %>% summarise(geometry = st_union(geometry)) %>% ungroup() # Aggregate your fake data to align with the region groups data_fake <- mapdata %>% select(code = `hc-a2`, region_group) %>% mutate(value = 1e5 * abs(rt(nrow(.), df = 10))) %>% group_by(region_group) %>% summarise(value = sum(value)) %>% ungroup()
Step 3: Plot with hc_add_series_map
Use hc_add_series_map to render the aggregated map with all your desired settings:
highchart() %>% hc_add_series_map( map = aggregated_map, data = data_fake, value = "value", joinBy = "region_group", name = "Fake data", # Show custom group names as data labels dataLabels = list( enabled = TRUE, format = '{point.region_group}', style = list(fontWeight = "bold", fontSize = "12px") ), # Add thick black borders around large regions borderColor = "#000000", borderWidth = 1.5, # Format tooltip for readability tooltip = list(valueDecimals = 2, prefix = "€ ") ) %>% hc_mapNavigation(enabled = TRUE) # Add zoom/pan controls for better interaction
Key Details:
- Why
hc_add_series_mapworks: This function supports custom map data (our aggregated large regions) instead of only pre-built maps, which is critical for creating custom region groupings. - Hidden internal borders: By aggregating geometries, we eliminate internal borders entirely—no extra steps needed to hide them, since they don't exist in the merged map.
- Flexibility: Tweak
borderWidth,borderColor, ordataLabelsstyle to match your preferred visual design.
内容的提问来源于stack exchange,提问作者MaRius

