2014-02-10 71 views
2

嗨我有一個矩陣37x73代表一個變量(moavg)與10x10度間距網格(-180 < LON < 180 e -90 < LAT < 90)。我可以使用圖像繪製它圖片與ggplot:如何繪製顏色傳說?

image(LON, LAT, moavg) 

但我無法顯示顏色欄。我想知道是否還有另一個功能可以做到這一點(也許是ggplot),讓我也可以繪製顏色圖例。

非常感謝

回答

3

對於繪製網格空間數據,rasterrasterVis包也是有用的。

這裏有幾個例子:

library(rasterVis) # this will also load the raster package 

# Create a raster from a matrix of dummy data 
m <- matrix(runif(36*18), ncol=36) 
r <- raster(m) 

# Set the extent of the object 
extent(r) <- c(-180, 180, -90, 90) 

# plot with raster 
plot(r) 

# plot with rasterVis 
levelplot(r, margin=FALSE) 

如果在一個文件中存在的網格數據(例如.ASC,.TIF等等),那麼你可以通過給raster()文件路徑加載它,例如raster('C:/path/to/moavg.asc'),你不應該在這種情況下設置範圍,因爲文件應該包含這個元數據。

有關更多詳細信息,請參閱?raster?levelplot

raster raster plot example

levelplot levelplot example


編輯

密謀解決分機繪圖ension這個問題在評論中發現,這裏有一個方法來覆蓋多邊形:

library(maps) 
levelplot(r, xlab='longitude', ylab='latitude', margin=FALSE, 
      panel = function(x, y, ...) { 
       panel.levelplot(x, y, ...) 
       mp <- map("world", plot = FALSE, fill=TRUE) 
       lpolygon(mp$x, mp$y) 
      }) 

levelplot with overlay

+0

嗨,這是偉大的!我如何設置座標軸名稱並覆蓋世界地圖?我試圖用地圖(加= T)蝙蝠它不工作.. – user3036416

+1

我會編輯我的答案,包括一個例子。 – jbaums

+0

嗨,而是根據網格域的間距來縮放地圖呢?謝謝 – user3036416

3

至於this answer

library(fields) 
image.plot(lon, lat, moavg, col=heat.colors(8)) 

enter image description here

PS:請這麼好心給你的問題提供了一個可重複的例子。 Here's how it works