rheatmapimage-scalingggimage

R Image scaling for heatmap


I am currently working on a problem with R. I'm pretty new to R so I lack of experience, on a (I guess) simple issue. I have a problem with scaling an image in relation to some data I have. The Image is a floor plan. The data I have is recorded manually.

the data looks like this: Data

My Code looks like this:

theData <-data.frame(EntryNr = c(0001,0002,0003,0004,0005,0006,0007), 
                 TimeSet = c('2017-01-15','2017-01-17','2017-01-18','2017-01-19','2017-01-20','2017-01-21','2017-01-22'),
                 SomeID = c('Mario','Mario','Mario','Luigi','Luigi','Luigi','Bowser'),
                 Room = c('Room1','Room1', 'Room1', 'Room1', 'Room1', 'Room1','Room1'),
                 theX = c(12.011, 11.767, 11.715, 11.827, 11.773,11.846,11.781), 
                 theY = c(6.733, 6.698, 6.871, 6.799, 6.887, 6.327,6.577),
                 theZ = c(3, 2.958, 2.983, 2.981, 2.992,2.952,2.945))

thePicture <- readPNG("FloorPlan.png")

ggimage(thePicture, fullpage = FALSE) +
  geom_point(aes(x = theX, y = theY, colour = SomeID), 
             data = theData, size = I(5), fill = NA) +
  labs(x='X axis', y='Y axis')

and my plot looks like this in the end (the background image is a simple floor plan): Plotted image from RStudio

So my problem now is, that the X and Y axis have really high scales. Y goes over 600 and X over 400. But according to the data, the dots should be seen in the plot in "Room1" (bottom right) instead of the bottom left.

Is there a way to rescale the picture?

Something like:

X axis scales from 0 to 15

Y axis scales from 0 to 25

the over-all aim of this, is to plot heatmaps from a lot more data I have, and show which ID was in which "room" for the most time.

EDIT: if I use the ggimage like this:

ggimage(thePicture, fullpage = FALSE, scale_axes = TRUE) +
  geom_point(aes(x = theX, y = theY,  colour = SomeID), 
             data = theData, size = I(5), fill = NA) +
  labs(x='X axis', y='Y axis') 

scale_axes = TRUE, my whole image shrinks to the bottom left corner and the scale goes from X = 0 to 12, Y = 0 to 7. so its still not the way i want it.


Solution

  • What you need is a scaling factor for each axis! For example you said the x axis ranges from 0 to 15 and the y axis from 0 to 25, the result would look like this...

    library(png)
    library(ggmap)
    
    thePicture <- readPNG('~/Desktop/Screen Shot 2016-12-01 at 13.03.23.png')
    y_factor <- dim(thePicture)[1] / 25
    x_factor <- dim(thePicture)[2] / 15
    
    theData <-data.frame(EntryNr = c(0001,0002,0003,0004,0005,0006,0007), 
                         TimeSet = c('2017-01-15','2017-01-17','2017-01-18','2017-01-19','2017-01-20','2017-01-21','2017-01-22'),
                         SomeID = c('Mario','Mario','Mario','Luigi','Luigi','Luigi','Bowser'),
                         Room = c('Room1','Room1', 'Room1', 'Room1', 'Room1', 'Room1','Room1'),
                         theX = c(12.011, 11.767, 11.715, 11.827, 11.773,11.846,11.781), 
                         theY = c(6.733, 6.698, 6.871, 6.799, 6.887, 6.327,6.577),
                         theZ = c(3, 2.958, 2.983, 2.981, 2.992,2.952,2.945))
    
    ggimage(thePicture, fullpage = FALSE) +
      geom_point(aes(x = theX * x_factor , y = theY * y_factor, colour = SomeID), 
                 data = theData, size = I(5), fill = NA) +
      labs(x='X axis', y='Y axis')
    

    Just use dim to get the dimensions of your png image. The first number is the y axis, the second the x axis. I don't know what the third number is... Use the ranges you mentioned for each axis and this should do the trick.