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

如何在R语言中将点云与图像绘制在同一图表中?

R语言实现图像与点云叠加绘制

基础绘图系统实现

你之前的问题在于两次调用plot()会创建新的绘图设备,导致生成两张独立图表。正确的做法是先初始化空绘图区域,再依次叠加图像和点云:

  1. 加载所需包并读取图片:
library(png)
Picture <- readPNG("image.png")
  1. 初始化空绘图区域,设置匹配图像的坐标范围(示例中图像放在x=1-50,y=1-50的区域):
plot(1:50, 1:50, type = "n", xlab = "", ylab = "")
  1. 添加图像到绘图区域:
rasterImage(Picture, xleft = 1, ybottom = 1, xright = 50, ytop = 50)
  1. 叠加点云数据(用points()而非plot(),避免新建画布):
points(mydata$x, mydata$y, pch = 16, col = "red")

ggplot2实现

你之前的ggplot2代码存在语法错误,正确方式是用annotation_raster添加图像,再结合geom_point绘制点云:

  1. 加载所需包:
library(png)
library(ggplot2)
Picture <- readPNG("image.png")
  1. 构建叠加图表:
ggplot() +
  # 添加图像,指定坐标范围
  annotation_raster(Picture, xmin = 1, xmax = 50, ymin = 1, ymax = 50) +
  # 添加点云数据
  geom_point(data = mydata, aes(x = x, y = y), color = "red", size = 2) +
  # 设置坐标范围匹配图像
  coord_cartesian(xlim = c(1, 50), ylim = c(1, 50))

关键注意事项

  • 确保图像的坐标范围和点云数据的坐标范围匹配,否则点可能显示在图像外。
  • 基础绘图中,rasterImage的参数顺序是图像对象, xleft, ybottom, xright, ytop,不要写错调用方式。
  • ggplot2中必须用annotation_raster而非直接调用rasterImage,后者无法在ggplot图层中正常工作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 21:33:16