You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中Highchart地图显示空白问题求助

地图气泡图空白问题排查与解决

我尝试用气泡图在地图上展示词汇出现频率,通过自有API导入数据后,完成了计数与数据合并操作,但绘图时地图输出为空白,其余步骤均正常执行。以下是我的实现代码:

library(httr)
library(jsonlite)
library(stringr)
library(highcharter)
library(dplyr)
library(highcharter)
library(tidytext)

zählen <- function(data, word) {
  result_grouped <- group_by(data, id, year, autor)
  result <- summarise(result_grouped, count = str_count(text, word))

  return(result)
}

res <- POST('https://api.textminr.tech/getTextByYearBetween/?minYear=1800&maxYear=1900')
data <- fromJSON(rawToChar(res$content))

data$lat <- NA
data$long <- NA

for (i in 1:nrow(data)) {
  author <- data$author[i]
  formatted <- URLencode(author)
  formatted_author <- gsub(",", "%2C", formatted)
  
  try({
    author_url <- str_glue('https://api.textminr.tech/getBirthplaceOfAuthor/?authorname={formatted_author}')
    res <- POST(author_url)
    authorData <- fromJSON(rawToChar(res$content))
  
    data$lat[i] <- authorData$lat
    data$long[i] <- authorData$long
  }, silent = TRUE)
}

# Ensure that lat and long are numeric
data$lat <- as.numeric(data$lat)
data$long <- as.numeric(data$long)

# Count the frequency of the word "war" in each text
word_counts <- data %>%
  unnest_tokens(word, text) %>%
  count(id, word) %>%
  filter(str_to_lower(word) == "war") %>%
  left_join(data, by = "id") %>%
  mutate(frequency = n)

# Create a list that highcharter can work with
map_data <- word_counts %>%
  mutate(name = author) %>%
  select(name, lat, long, frequency) %>%
  list_parse()

# Create the map
wmap <- highchart() %>% 
  hc_add_series(
    data = map_data,
    type = "mapbubble",
    name = "Word Frequency",
    maxSize = '10%'
  ) %>%
  hc_mapNavigation(enabled = TRUE)

# Print the map
wmap

该API对所有用户开放,可用于测试功能。我还尝试了一段示例代码,同样遇到地图输出空白的问题:

library(highcharter)
library(dplyr)

# Here's some example data with countries and values
data_countries <- data.frame(
  code = c('DE', 'US', 'FR', 'ES', 'IT'),
  value = c(4, 5, 3, 7, 6)
)

# First, let's get the map data for the world
data_world <- get_data_from_map(download_map_data('custom/world'))

# Now, let's create the highchart map
highchart(type = "map") %>% 
  hc_add_series(data = data_world, mapData = data_world, joinBy = "hc-key", name = "Basemap") %>% 
  hc_add_series(data = data_countries, type = "mapbubble", mapData = data_world, joinBy = c("iso-a2", "code"), name = "Countries", maxSize = '12%') %>% 
  hc_mapNavigation(enabled = TRUE) # Add navigation buttons

核心问题与修复方案

1. 缺失基础地图图层

mapbubble是叠加在基础地图上的图层,原代码没有加载底图,导致画布空白。必须先添加基础世界地图,再叠加气泡层。

2. 数据映射与格式问题

  • 经纬度类型的气泡图需要明确指定lat/lon字段映射
  • 过滤掉无有效经纬度的无效数据,避免干扰渲染

修复后的主代码

library(httr)
library(jsonlite)
library(stringr)
library(highcharter)
library(dplyr)
library(tidytext)
library(glue) # 加载str_glue依赖包

zählen <- function(data, word) {
  result_grouped <- group_by(data, id, year, autor)
  result <- summarise(result_grouped, count = str_count(text, word))

  return(result)
}

res <- POST('https://api.textminr.tech/getTextByYearBetween/?minYear=1800&maxYear=1900')
data <- fromJSON(rawToChar(res$content))

data$lat <- NA
data$long <- NA

for (i in 1:nrow(data)) {
  author <- data$author[i]
  formatted <- URLencode(author)
  formatted_author <- gsub(",", "%2C", formatted)
  
  try({
    author_url <- glue('https://api.textminr.tech/getBirthplaceOfAuthor/?authorname={formatted_author}')
    res <- POST(author_url)
    authorData <- fromJSON(rawToChar(res$content))
  
    data$lat[i] <- authorData$lat
    data$long[i] <- authorData$long
  }, silent = TRUE)
}

# 转换为数值类型并过滤无效经纬度
data$lat <- as.numeric(data$lat)
data$long <- as.numeric(data$long)
data <- data %>% filter(!is.na(lat), !is.na(long))

# 统计"war"的出现频率
word_counts <- data %>%
  unnest_tokens(word, text) %>%
  count(id, word) %>%
  filter(str_to_lower(word) == "war") %>%
  left_join(data, by = "id") %>%
  mutate(frequency = n)

# 创建地图:先加底图,再加气泡
wmap <- highchart(type = "map") %>% 
  # 添加基础世界地图
  hc_add_series(
    mapData = download_map_data('custom/world'),
    name = "底图",
    nullColor = "rgba(200, 200, 200, 0.3)",
    borderColor = "#FFFFFF"
  ) %>%
  # 添加气泡层,指定经纬度和数值映射
  hc_add_series(
    data = word_counts,
    type = "mapbubble",
    name = "词汇出现频率",
    maxSize = '10%',
    mapping = hcaes(lat = lat, lon = long, z = frequency)
  ) %>%
  hc_mapNavigation(enabled = TRUE)

# 显示地图
wmap

修复后的示例代码

library(highcharter)
library(dplyr)

# 示例国家数据
data_countries <- data.frame(
  code = c('DE', 'US', 'FR', 'ES', 'IT'),
  value = c(4, 5, 3, 7, 6)
)

# 获取世界地图数据
data_world <- get_data_from_map(download_map_data('custom/world'))

# 创建地图
highchart(type = "map") %>% 
  hc_add_series(
    data = data_world, 
    mapData = data_world, 
    joinBy = "hc-key", 
    name = "底图",
    nullColor = "rgba(200, 200, 200, 0.3)",
    borderColor = "#FFFFFF"
  ) %>% 
  hc_add_series(
    data = data_countries, 
    type = "mapbubble", 
    mapData = data_world, 
    joinBy = c("iso-a2", "code"), 
    name = "国家数据", 
    maxSize = '12%',
    dataLabels = list(enabled = TRUE, format = '{point.name}')
  ) %>% 
  hc_mapNavigation(enabled = TRUE)

内容的提问来源于stack exchange,提问作者Nicolas Pfeiler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:04:51