lunedì 19 gennaio 2009

Map coordinates to actual pixel locations on a PNG device

Jason emailed me a new tip. Enjoy it!

Use grconvertX and grconvertY to map the X,Y coordinates for an entity on a graphics device to user coordinates. For example if you plotted points to an image and wanted to map those X,Y coordinates to the actual pixel locations on the PNG you would use this family of functions.

#
# Sample R Code for grconvertX and grconvertY
#

# make fake data
tDat <- cbind(rnorm(10), rnorm(10));

#
# Example #1 -- plot them to an X11 window
#
x11();
plot(tDat);
print(paste(grconvertX(tDat[, 1], "user", "device"), grconvertY(tDat[, 2], "user", "device")));

# turn off the x11 device
#dev.off()


#
# Example 2-- Get the pixel coordinates of the data on a PNG image
#
# plot to a PNG
png(file="RTip_coordinates.png", height=1000, width=1000);
plot(tDat);
print( paste(grconvertX(tDat[, 1], "user", "device"), grconvertY(tDat[, 2], "user", "device")));
dev.off()

# Now, go into GIMP or photoshop. At each data point should be at the
# X,Y coordinate listed.

3 commenti:

  1. So the process is, give your coordinates, plot wathever function you need and then get back the device coordinates?
    This tip probably is what I was looking for create HTML-ImageMaps.

    --
    Ra

    RispondiElimina
  2. I'd like to take this opportunity to thank Jason for the useful tips he shared with the R community! Thank you man!

    RispondiElimina