0

My goal is to create a heatmap on top of a human stencil.

The data looks like this:

structure(list(w = c("A", "B", "C", "D", "E"
), f1 = c(129, 111, 128, 129, 126), f2 = c(55, 133, 55, 127, 
56)), row.names = c(46L, 111L, 113L, 115L, 118L), class = "data.frame")

I followed these instructions on Stack Overflow and therefore wrote this code:

ggplot(df, aes(f1,f2))  + 
  annotation_raster(traf, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)+
  stat_density2d(geom = "polygon", aes(fill=..level..)) + 
  geom_point(size=0.5)+
  scale_fill_gradient(low="green",high="red") + 
  scale_x_continuous(limits=c(0,dim(traf)[2]),expand=c(0,0))+
  scale_y_continuous(limits=c(0,dim(traf)[1]),expand=c(0,0))+
  coord_fixed()

Those lines of code produced this image: enter image description here

Obviously I would need the image to retain its original size so that the points would correspond accordingly to the picture of the human outline. It would be expected that the points would occupy space around the face, neck, and stomach. I am a bit unsure why the offset has occurred because both f1 (x) and f2 (y) were produced on the same picture and no processing or aggregating has been done neither to the data nor the image.

What can I do to a) make the image the original size b) plot the heatmap correctly on top of the human stencil?

The original image is here:

enter image description here

traf <- load.image("50perc.jpg")
plot(traf)

produces following (if that helps): enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
vbadvd
  • 21
  • 7
  • It seems that your aspect ratio for the graphics device does not match that of your picture. If I ran the example you posted, my graphics device image is stretched too, because the height and width of your plot are calculated "on the fly", so to speak. That is, they are created based upon your graphics device window and resolution. I found that the [answer posted here](https://stackoverflow.com/questions/16422847/save-plot-with-a-given-aspect-ratio) can be quite helpful to specifically match the plot to the aspect ratio of your image. Not sure how well it works with a legend. – chemdork123 Aug 19 '20 at 21:35
  • 2 things for consideration. 1) Try switching `dim(traf)[1]` and `dim(traf)[2]` in your `scale_*()` lines. I think the first value in `dim` refers to the image's x-dimension and the second to its y-dimension, not vice versa. 2) Check if the values used in `df` were taken with the top left corner of the image as the (0, 0) point. – Z.Lin Aug 20 '20 at 03:57

0 Answers0