Defining polygon coordinates with a predefined image and plotting in R

#1
I have an image and want to plot on specific rectangle-shaped parts of it. I would like to be able to easily specify the coordinates for these parts and take these coordinates into R for plotting. More specifically, I want to plot a heatmap on a store blueprint. Here is an example of the plot I want to achieve. Each shelf should be colored by a different color, depending on how many shoppers visit it.

I don't have any experience with making such plots. I've thought of simply inserting an image into a plot with rasterImage (), then plotting with polygon (), but the task of setting up the coordinates for the polygon function seems to be too time-consuming. If anybody has any other ideas of how to do this, I would be very thankful for that. Using a program other than R might also be an option, so any leads are helpful: packages, function, programs.

Also, do you know if I can somehow specify the coordinates by referring to the image pixels? It is much easier for me to find out the pixel coordinates for the key areas than to use the arbitrary axes in R.

Thank you. Marko.
 
#2
I got an answer at a different forum so I though about posting it here. Using the locator () function enables you to specify coordinates easily as you can just click on the plot with the picture on it and get x,y coordinates back. These coordinates can be stored in a variable and then passed on to other functions. Below is an example of how to do it (you will need to input your own picture to try it out).

library (png)
img <- readPNG ("your_image.png") # storing the image

plot (1:100, type="n", axes=F, xlab="", ylab="") # ploting an empty plot
lim <- par ()
rasterImage (img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4]) # plotting the image; you use lim$usr to plot over the whole plotting area

leftBottom <- locator ()
rightTop <- locator () # these are the key steps where you define the locations for your other plotting elements by clicking on the image in the plotting area; here I am taking two x,y coordinates so I can define rectangles

rect (xleft=leftBottom$x, ybottom=leftBottom$y, xright=rightTop$x, ytop=rightTop$y, col=rgb (PalHeatmap (input) / 255, alpha = 0.5)) # the positions of the rectangles are defined by the outputs of the locator function