2

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

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.

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

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.

Any help is much appreciated.

EDIT: here are some example data and I attached the imageenter image description here:

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))
Btibert3
  • 38,798
  • 44
  • 129
  • 168
  • Can you give an example of what you did and what it looks like and what you actually want to achieve. I do not get it. – Henrik Feb 28 '12 at 16:16
  • You can certainly set image coordinates with the `rasterimage` tools, but I suspect there's an easier way to locate a "background image" for plots. I'll go look around. – Carl Witthoft Feb 28 '12 at 16:52
  • Isn't this what `rasterImage` does? See the examples where it places an image at specific coords. – Charles Feb 28 '12 at 18:14
  • 1
    Well, now your problem is clear: you left out the Bruins logo at center ice! – Carl Witthoft Feb 29 '12 at 12:23

1 Answers1

3

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.

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.

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) )

Does this do what you want?

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • I was unaware of rasterImage, and with your code example, I was able to get it to work. Many thanks! – Btibert3 Mar 02 '12 at 01:49