1

I have imported a jpeg and I would like to put data over the top of it (the picture is a habitat and I have data of movement of an animal living in the area). I am hoping to create lines that correspond to where the animal has been recorded.

So far, I have imported the image using 'readJPEG', and have visualized my data this way (img = my imported jpeg):

plot(1, type="n", xlim=c(100, 150), ylim=c(300, 350)) 
rasterImage(img,100, 300, 150, 350, interpolate = TRUE)  

Any help on how to plot data on top of this photo? I am hoping to simply use the coordinates already in place when I visualize the data (that is, the x and y tick labels indicated above).

Thank you!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Katie
  • 323
  • 3
  • 10
  • 4
    So what exactly do you want to add? You should be able to add whatever you want with `points()`, `lines()`, `text()`, etc... – MrFlick Jun 19 '17 at 21:22
  • Possible duplicate of [In R, how to plot with a png as background?](https://stackoverflow.com/questions/12918367/in-r-how-to-plot-with-a-png-as-background) – M-- Jun 19 '17 at 21:53
  • Or this: https://stackoverflow.com/questions/4993189/overlay-data-onto-background-image-in-r – M-- Jun 19 '17 at 21:53
  • Or this one: https://stackoverflow.com/questions/5073386/how-do-you-directly-overlay-a-scatter-plot-on-top-of-a-jpg-image-in-matplotlib – M-- Jun 19 '17 at 21:53

1 Answers1

0

You might find functions from the Bioconductor package EBImage helpful for working with your images, as they spare you the tedious task of setting the correct coordinates etc. Once you load the image with readImage and display it, you can use R base graphics functions such as points, lines, text and similar to add things on top. Coordinates are set to image pixel coordinates with the origin (1,1) in the top left corner. The following example is taken from the package vignette.

library("EBImage")

f = system.file("images", "sample.png", package="EBImage")
img = readImage(f)

display(img, method="raster")
text(x = 20, y = 20, label = "Parrots", adj = c(0,1), col = "orange", cex = 2)

enter image description here

aoles
  • 1,525
  • 10
  • 17