[英]Image vs ggplot: how to plot color legend?
嗨,我有一個矩陣37x73代表一個變量(moavg)網格,間距為10x10度(-180 <LON <180 e -90 <LAT <90)。 我能夠使用圖像繪制它
image(LON, LAT, moavg)
但我無法顯示顏色條。 我想知道是否還有其他功能(也許是ggplot),這也允許我繪制顏色圖例。
非常感謝
對於繪制網格化空間數據, raster
和rasterVis
包也很有用。
以下是幾個例子:
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
繪圖
用levelplot
繪圖
編輯
為了解決評論中對此問題的擴展,這里有一種覆蓋多邊形的方法:
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)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.