2014-11-04 130 views
0

我想製作一個簡單的日期*溫度熱圖(?柵格圖?),它顯示了基於裝倉溫度組的溫度隨時間的變化。就像this一樣,但是隻能沿着日期軸(沒有時間變量),我更喜歡使用ggplot,但是一直運行時誤入歧途。數據產生的圖形朝着正確的方向發展,但我無法弄清楚如何擺脫y軸的。我想感謝所有幫助簡單?熱圖?溫度在R(ggplot2)

dat <- data.frame(temp = sample(20,100, replace=TRUE), date=seq(as.Date("2011-07-01"), by=1, len=100)) 

p <- ggplot(dat, aes(date, temp)) + geom_tile(aes(fill = temp)) + scale_fill_gradient(low = "blue", high = "red") 

謝謝!

回答

1

所以你不想在y軸上地圖temp

那麼,你可以使用一個固定的y的值並移除其餘的y軸:

dat <- data.frame(temp = sample(20,100, replace=TRUE), 
        date=seq(as.Date("2011-07-01"), by=1, len=100)) 
require(ggplot2) 
ggplot(dat, aes(x = date, y = 1)) + 
    geom_tile(aes(fill = temp)) + 
    scale_fill_gradient(low = "blue", high = "red") + 
    labs(y = NULL) + 
    scale_y_continuous(breaks = NULL) 

enter image description here

+0

謝謝了!所以設置y = 1是個訣竅。現在感覺有點笨... – 2014-11-04 11:57:56

+0

如果你喜歡我的回答,你可以投票並將其標記爲已接受。 – EDi 2014-11-04 20:26:57

-1

您也可以嘗試做一些像下面與metvurst包的情節。

http://i.imgur.com/8Js1Uz7.png

dat <- data.frame(temp = sample(20,60, replace=TRUE), 
     date=seq(as.POSIXct("2011-01-01 00:00"), by=3600, len=8760)) 

dat$year <- as.numeric(format(dat$date,"%Y")) 

dat$month <- as.numeric(format(dat$date,"%m")) 

# Install and load metvurst library 
install_github('metvurst', 'tim-salabim') 
library(metvurst) 

plot.air.temp <- strip(x = dat$temp, 
       date = dat$date, 
       cond = dat$year, 
       arrange = "long", 
       colour = colorRampPalette(rev(brewer.pal(11, "Spectral"))), 
       main = "Daily Air Temperatures\n\nTemperature [°C]") 

plot.air.temp