開始使用免費開始

使用 Raster 物件的套件

你已經看到 tmap 套件能輕鬆視覺化 sp 中的空間類別。好消息是,它同樣適用於 raster 的類別!你只要把 Raster___ 物件作為 tm_shape() 函式的 shp 參數傳入,接著加上一個 tm_raster() 圖層,如下所示:

tm_shape(raster_object) +
    tm_raster()

當你處理 RasterStack 或 RasterBrick 物件時(例如你在上一個練習建立的 pop_by_age),可以在 tm_raster() 中使用 col 參數(「color」的縮寫),並將圖層名稱加上引號來顯示其中一個圖層。

在整個課程中你都會使用 tmap,但我們也想讓你看看另一個專門用來視覺化 raster 物件的套件 rasterVis。rasterVis 有幾個不同的函式可以用來作圖,不過先試其中一個:levelplot()。

本練習屬於課程

在 R 視覺化地理空間資料

檢視課程

練習說明

  • 使用 tmap 繪製 pop 物件:在 tm_shape() 的 shp 參數指定 pop,並加上一個 tm_raster() 圖層。
  • 使用 tmap 繪製 pop_by_age(一個 RasterStack 物件)中的 under_1 圖層。
  • 對 pop 呼叫 rasterVis 的函式 levelplot()。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

library(tmap)

# Specify pop as the shp and add a tm_raster() layer
tm_shape(___) +
  ___

# Plot the under_1 layer in pop_by_age



library(rasterVis)
# Call levelplot() on pop
編輯並執行程式碼