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

如何在R中使用RAmCharts包(或同类优质包)实现蜂窝状瓷砖地图?

R语言实现蜂窝状瓷砖地图

一、使用RAmCharts4包(匹配参考示例的交互式效果)

RAmCharts4是amCharts JavaScript库的R语言绑定,能完美复刻你参考的交互式蜂窝瓷砖地图效果,步骤如下:

1. 安装并加载包

# 安装包
install.packages("ramcharts4")
# 加载库
library(ramcharts4)

2. 准备数据

需要包含区域标识、数值等核心字段的数据集,这里模拟示例风格的区域数据:

# 模拟蜂窝区块数据
honeycomb_data <- data.frame(
  id = c("A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"),
  name = c("区域1", "区域2", "区域3", "区域4", "区域5", "区域6", "区域7", "区域8", "区域9"),
  value = sample(10:100, 9, replace = TRUE)
)

3. 构建交互式蜂窝地图

通过amChart()创建基础图表,指定六边形系列,并配置颜色映射、交互提示和标签:

# 创建蜂窝瓷砖地图
amChart(honeycomb_data, "HexagonalSeries") %>%
  amSeries(
    dataFields = list(category = "id", value = "value"),
    heatRules = list(
      list(
        target = "polygon",
        property = "fill",
        min = "#e0f7fa",
        max = "#00796b",
        dataField = "value"
      )
    ),
    tooltip = list(
      text = "{name}: {value}"
    ),
    label = list(
      text = "{name}",
      fontSize = 12
    )
  ) %>%
  amLegend() %>%
  amTitle(text = "蜂窝状瓷砖地图示例")

运行后会生成和参考示例风格一致的交互式地图:鼠标悬停显示数值,区块颜色随数值深浅渐变,支持缩放、拖拽等操作。

二、替代方案:使用ggplot2 + ggforce实现静态/半交互式蜂窝图

如果偏好ggplot2生态,可结合ggforce包绘制蜂窝状瓷砖:

1. 安装并加载依赖包

install.packages(c("ggplot2", "ggforce", "dplyr"))
library(ggplot2)
library(ggforce)
library(dplyr)

2. 生成蜂窝布局坐标

先计算每个六边形的排列坐标,模拟蜂窝网格:

# 生成蜂窝网格坐标
hex_coords <- data.frame(
  x = rep(c(0, 1, 2), 3) + rep(c(0, 0.5, 0), each = 3),
  y = rep(c(0, 0.866, 1.732), each = 3),
  name = honeycomb_data$name,
  value = honeycomb_data$value
)

3. 绘制静态蜂窝图

ggplot(hex_coords, aes(x0 = x, y0 = y, fill = value)) +
  geom_regon(
    sides = 6,
    radius = 0.45,
    colour = "white",
    size = 1
  ) +
  geom_text(aes(x = x, y = y, label = name), size = 4) +
  scale_fill_viridis_c(option = "plasma") +
  theme_void() +
  labs(title = "静态蜂窝瓷砖地图", fill = "数值") +
  theme(plot.title = element_text(hjust = 0.5))

若需要交互式效果,可通过plotly::ggplotly()将静态图转为可交互版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 22:13:33