将数据叠加到R中的图像上的参考点 [英] Set Reference Points for Overlaying Data onto an image in R

查看:95
本文介绍了将数据叠加到R中的图像上的参考点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用R绘制图像,我在这里看到了一些问题(一个甚至是我)。

I have seen a few questions on here (one was even by me) with respect to using R to plot an image.

这里的区别在于我需要为我的图像设置参考cooridnates以匹配我想要在图像顶部绘制的数据。

The difference here is that I need to set the reference cooridnates for my image to match the data I am looking to plot on top of the image.

更具体地说,我需要R来理解背景图像的坐标是 x =( - 100,100) y =(40,-40)

More specifically, I need R to understand that the coordinates for the background image are x = (-100,100) and y = (40,-40).

我已经能够读入图像文件了使用 ReadImages 包绘制它,但是当我使用 points()覆盖我的数据时,数据显然不符合适当的。

I have been able to to read in the image file and plot it using the ReadImages package, but when I overlay my data using points(), the data obviously do not line up appropriately.

非常感谢任何帮助。

编辑:这是一些示例数据,我附上图片:

here are some example data and I attached the image:

structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2", 
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20", 
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79", 
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53", 
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59", 
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18", 
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25", 
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16", 
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"), class = "data.frame", row.names = c(74328L, 
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L, 
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L, 
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L, 
100163L, 100167L))


推荐答案

您可以使用正确的尺寸创建一个空图,然后使用 rasterImage 绘制图像的功能,然后添加点应该可以正常工作。

You can create an empty plot with the correct dimensions, then use the rasterImage function to plot the image, then adding the points should work fine.

另一种方法是使用 updateusr 在绘制图像后,从 TeachingDemos 包中输入函数,以确保在添加线条或点之前坐标符合您的要求。

Another approach is to use the updateusr function from the TeachingDemos package after plotting the image to make sure that the coordinates match what you want them to before adding lines or points.

我将您的图片保存为ice.png并运行以下代码:

I saved your graphic above as ice.png and ran the following code:

library(EBImage)
ice <- readImage('My Pictures/ice.png')
pos <- structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
  "6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
  "10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
  "81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
  "54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
  "50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
  "-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
  "-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
  "8", "8")), .Names = c("teamid", "xcoord", "ycoord"),
 class = "data.frame", row.names = c(74328L,
  74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
  74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
  100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
  100163L, 100167L)) 
pos$xcoord <- as.numeric(pos$xcoord)
pos$ycoord <- as.numeric(pos$ycoord)

ice2 <- as.raster(ice)

pin <- par('pin')
plot( c(-100,100), c(-40,40), type='n', xlab='', ylab='', 
    asp=pin[1]/pin[2], axes=FALSE, xaxs='i', yaxs='i')
rasterImage(ice2, -100, -40, 100, 40, interpolate=FALSE)
with(pos, text(xcoord, ycoord, teamid, col='green', cex=1.2) )

这样做你想要的吗?

这篇关于将数据叠加到R中的图像上的参考点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆