[英]R Image scaling for heatmap
我目前正在研究R的一個問題。我對R很新,所以我缺乏經驗,我(我猜)是一個簡單的問題。 我有一個與我的一些數據相關的縮放圖像的問題。 圖像是平面圖。 我手動記錄的數據。
數據如下所示: 數據
我的代碼看起來像這樣:
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')
我的情節最終看起來像這樣(背景圖片是一個簡單的平面圖): 來自RStudio的繪制圖像
所以我現在的問題是,X軸和Y軸確實很高。 Y超過600,X超過400.但根據數據,應在“Room1”(右下角)而不是左下角的圖中看到點。
有沒有辦法重新縮放圖片?
就像是:
X軸從0到15
Y軸從0到25
這個目的的總體目標是從我擁有的更多數據中繪制熱圖,並顯示哪個ID在哪個“房間”最長時間。
編輯:如果我像這樣使用ggimage:
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 ,我的整個圖像縮小到左下角,比例從X = 0到12,Y = 0到7.所以它仍然不是我想要的方式。
您需要的是每個軸的縮放系數! 例如,你說x軸的范圍是0到15,y軸的范圍是0到25,結果看起來像這樣......
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')
只需使用dim
來獲取png圖像的尺寸。 第一個數字是y軸,第二個數字是x軸。 我不知道第三個數字是什么...使用你提到的每個軸的范圍,這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.