您當前的位置:首頁 > 攝影

R語言空間資料分析(九):柵格資料探索性分析

作者:由 HopeR 發表于 攝影時間:2020-11-13

作者:黃天元,復旦大學博士在讀,熱愛資料科學與開源工具(R),致力於利用資料科學迅速積累行業經驗優勢和科學知識發現,涉獵內容包括但不限於資訊計量、機器學習、資料視覺化、應用統計建模、知識圖譜等,著有《R語言高效資料處理指南》(

《R語言資料高效處理指南》(黃天元)【摘要 書評 試讀】- 京東圖書

)。知乎專欄:

R語言資料探勘。

郵箱:[email protected].歡迎合作交流。

EDA(Exploratory Data Analysis,探索性資料分析)是資料科學重要的一個環節,對於給定的柵格資料,我們要如何進行視覺化呢?本帖子將會基於HopeR:R語言空間資料分析(八):柵格資料預處理中清洗好的資料,進行探索性資料分析。首先,我們把資料匯入R環境中。這裡,我們只對1到7共七個波段進行分析。

library(pacman)

p_load(sp,raster,rgeos,rgdal,rasterVis,raster,fs,sf,tidyverse)

dir(“data/LC08_L1TP_203023_20190513_20190521_01_T1/mask”,full。names = T) %>%

str_subset(pattern = “_B[1-7]_”) %>%

stack() -> manc

在manc變數中,波段是按照1到7的順序排列的,我們根據波段的光譜名稱,給這些圖層進行命名:

# Name the Bands based on where they sample the electromagentic spectrum

names(manc) <- c(‘ultra-blue’, ‘blue’, ‘green’, ‘red’, ‘NIR’, ‘SWIR1’, ‘SWIR2’)

然後,我們可以對manc這個變數進行一些基本的探索:

crs(manc) # projection 座標系

extent(manc) # extent 分佈範圍

ncell(manc) # number of cells 格點數量

dim(manc) # number of rows, columns, layers 資料維度

nlayers(manc) # number of layers 圖層個數

res(manc) # xres, yres 畫素是多少乘多少的

R語言空間資料分析(九):柵格資料探索性分析

然後,讓我們來對圖層進行視覺化。可以利用紅綠藍這三個光譜圖層合成真彩色影象,也可以利用其它光譜圖層合成偽彩色影象,關於這方面的知識,可以參考遙感影像圖中真彩色,假彩色,偽彩色是怎樣定義的?為什麼要分這些類?。這裡我們直接用程式碼進行實現:

# visualization

# true colour composite

manc_rgb <- stack(manc$red, manc$green, manc$blue)

# false colour composite

manc_false <- stack(manc$NIR, manc$red, manc$green)

manc_rgb %>%

plotRGB(。,axes=TRUE, stretch=“lin”)

manc_false %>%

plotRGB(。,axes=TRUE, stretch=“lin”)

R語言空間資料分析(九):柵格資料探索性分析

真彩色影象

R語言空間資料分析(九):柵格資料探索性分析

偽彩色影象

我們可以只觀察一個波段的圖層,比如SWIR2:

plot(manc$SWIR2)

也可以同時觀察4個圖層,基本包中的繪圖函式可以這樣實現:

## How are these bands different?

#set the plot window size (2 by 2)

par(mfrow = c(2,2))

#plot the bands

plot(manc$blue, main = “Blue”)

plot(manc$green, main = “Green”)

plot(manc$red, main = “Red”)

plot(manc$NIR, main = “NIR”)

R語言空間資料分析(九):柵格資料探索性分析

最後,我們可以嘗試使用GGally包的ggpairs函式來探知不同光譜之間的相關性(只進行抽樣調查,抽了100個樣本):

library(ggplot2)

library(GGally)

manc %>%

as。data。frame(。, na。rm=TRUE)%>%

sample_n(。, 100)%>%

ggpairs(。,axisLabels=“none”)

R語言空間資料分析(九):柵格資料探索性分析

對角線上是單變數的分佈狀況,左下角為散點圖,右上角是變數兩兩之間的相關係數,星號*代表顯著水平,使用的是Pearson相關係數(參考自Correlation matrix with ggally)。

標簽: manc  圖層  PLOT  main  彩色影象