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

如何将XY坐标叠加到身体图像生成热力图(R/Python/JS)

身体疼痛位置热力图实现方案(R语言)

需求概述

我有一份在线调研导出的数据集(样本量n=256),每条记录包含参与者在身体图上标记的疼痛位置XY坐标(每位参与者最多提供10组坐标对)。需要将这些坐标叠加到597×597的身体图像上,生成热力图可视化效果。此前尝试的在线教程仅支持谷歌地图坐标叠加,因此需要用R语言实现自定义图像的热力图。

数据子集示例

ID  x1  y1  x2  y2  x3  y3  x4  y4  x5  y5  x6  y6  x7  y7  x8  y8  x9  y9  x10 y10
1   467 77  135 121 104 265 135 170 155 481 441 406 488 221 462 279 541 300 390 299
2   495 464 476 520 486 488 129 436 130 468 156 262                             
3   133 274 147 259 117 256                                                     
4   132 140 451 89  493 101 123 92  157 89  123 240 155 243 440 296 509 297     
5   156 495 490 499 442 495 121 502 158 269                                     
6   123 244 151 256 158 151 175 189 434 419 472 496 431 353 394 189 414 149     
7   134 100 135 127 112 163                                                     
8   95  304 509 301 118 271 149 269                                             
9   136 247 148 148 165 194 153 506 153 320 127 200 132 123 439 533             
10  130 135 177 324 162 294 141 255                                             
11  146 249 490 307 507 165 411 167 397 239 490 406 444 400 427 300 204 284 54  277
12  440 415 482 415 535 336 454 530 439 527                                     
13  105 497 177 162 186 195 481 492 420 149 421 191 165 272 158 314 435 283 435 311
14  153 254 100 382 145 76  145 133 197 230 437 195 452 302 466 146             
15  379 331 553 328 218 292 54  296 52  333 227 334 385 299 545 301 443 416     
16  135 113 467 87  131 251 149 262 222 323 209 286 151 139 133 172             
17  121 110 139 109 107 143 172 145 451 137 480 136 140 162 123 165 123 131 147 135
18  131 129 172 152 105 157 151 250 122 252 168 288 110 298 487 136 451 137     
19  448 406 500 153 437 158 472 93  459 307 446 303 431 281 422 293 489 500 452 495
20  145 252 468 143 429 161 506 166 175 145 131 145 466 69  138 74             
21  480 289 116 279 124 271 458 285 472 148 124 148 148 141 157 265             
22  84  313 427 304 503 309 182 309 510 172 412 168 126 232 128 133 493 414 444 414
23  452 110 431 163 440 310 461 316 145 278 137 73  459 90  144 118 159 145 145 313
24  118 277 153 276 130 125 126 84  139 525 458 83  444 507 485 504 441 284 492 274
25  503 287 389 296 546 293 426 287 488 404 447 498 491 497 135 177 500 151 433 151
26  476 397 448 400 109 142 83  228 126 527                                     
27  150 499 123 503 500 219 432 222 511 177 417 182 451 411 491 411 452 532 489 539
28  441 410 485 408 208 273 63  273 177 158                                     
29  163 410 148 409 435 415 444 404                                             
30  446 403 485 407 480 532 447 527 561 341 501 301 113 260 374 336             
31  441 407 153 409 133 255                                                     
32                                                                                
33  148 133 492 416 441 412 115 264 498 449 477 443 451 423 495 356 178 150 99  276
34  499 454 497 468 494 479 495 491 144 535 148 524 122 519 134 128 151 257 124 259
35  454 192 457 288 489 503 558 288 323 294 141 272 133 205 131 168 131 508 141 135

资源信息

  • 身体图像尺寸:597×597像素
  • 完整CSV数据集包含256条样本记录

R语言实现代码

1. 安装并加载依赖包

# 首次运行时安装所需包
install.packages(c("tidyverse", "ggplot2", "magick"))

# 加载核心包
library(tidyverse)
library(ggplot2)
library(magick)

2. 数据读取与格式转换

将宽格式的坐标数据转为长格式,方便后续可视化处理:

# 读取CSV文件(替换为你的本地文件路径)
pain_data <- read.csv("bodymap.csv", header = TRUE, na.strings = "")

# 转换为长格式,过滤无效空值
long_data <- pain_data %>%
  pivot_longer(cols = -ID, names_to = c(".value", "pair"), names_pattern = "(x|y)(\\d+)") %>%
  drop_na(x, y) %>%
  mutate(pair = as.integer(pair))

3. 读取身体图像

# 读取本地身体图像文件(替换为你的图像路径)
body_img <- image_read("body_map.png")

4. 生成热力图

# 获取图像尺寸信息
img_dim <- image_info(body_img)
img_width <- img_dim$width
img_height <- img_dim$height

# 绘制热力图
pain_heatmap <- ggplot() +
  # 叠加身体图像作为背景
  annotation_raster(body_img, xmin = 0, xmax = img_width, ymin = 0, ymax = img_height) +
  # 添加2D密度热力层,调整透明度控制叠加效果
  stat_density_2d(data = long_data, aes(x = x, y = y, fill = ..level..), 
                  geom = "polygon", alpha = 0.5, h = c(15, 15)) +
  # 设置热力颜色渐变(可根据需求替换其他配色)
  scale_fill_viridis_c(option = "inferno", name = "疼痛密度") +
  # 匹配图像坐标轴范围
  xlim(0, img_width) +
  ylim(0, img_height) +
  # 反转Y轴:适配图像左上角为原点的坐标规则
  scale_y_reverse() +
  # 隐藏冗余元素,保持可视化简洁
  theme_void() +
  theme(legend.position = "right")

# 预览结果
print(pain_heatmap)

# 保存高清热力图(可调整dpi参数控制分辨率)
ggsave("pain_heatmap.png", pain_heatmap, width = img_width/300, height = img_height/300, dpi = 300)

关键参数说明

  • 坐标反转:图像默认Y轴原点在左上角,而ggplot默认原点在左下角,scale_y_reverse()确保坐标匹配。
  • 平滑度调整:stat_density_2d的h参数控制热力平滑程度,数值越小细节越清晰,越大则越模糊。
  • 透明度控制:alpha参数调整热力层与背景图像的叠加透明度,建议设置在0.4-0.6之间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 18:07:04