R語言處理.nc檔案(land cover)繪製土地覆蓋變化趨勢圖
一.資料準備
NC檔案指的是土地利用(Land use and land cover)資料(全球尺度),一般從網上就能下載,這裡提供幾個下載地址,時間跨度為850-2018:
1.土地使用狀態資料(具體類別下面會講)
2.土地型別轉移資料
3.土地管理資料
直接點選連結就會下載。這裡以最常用的土地使用狀態資料為例繪製林地和農田的變化趨勢圖。
二、資料處理
1.資料下載後格式為nc,可以放到R的工作目錄下,或者在一開始設定工作目錄為nc檔案所在資料夾。比如我放在了E盤的rroom資料夾下:
資料處理部分的程式碼如下:
setwd("E:/rroom") #當前工作目錄
#需要用到的包
library(RColorBrewer)
library(lattice)
library(trend)
library(magrittr)
library(raster)
library(ggplot2)
library(rnaturalearth)
library(ncdf4)
library(cowplot)
library(dplyr)
rm(list = ls())
#***********************資料處理***************************
ncdata <- nc_open("states.nc")
開啟nc檔案後可以看到具體的值,維度為3,1維是緯度,二維是經度,三維是時間,年份為850-2018,可根據自己的需要切割。
states檔案中主要包括14種土地型別
可以點開檢視全名,這次的林地用到原始林地資料(primf)和次生林地(secdf),農田用到C3類一年生作物(c3ann)和C4(c4ann)類一年生作物。
lon <- ncvar_get(ncdata,'lon') #ncvar_get get the value data
lat <- ncvar_get(ncdata,'lat') # get longitude and latitude
time<-ncvar_get(ncdata,'time') # time from 1 to 1169
primf<- ncvar_get(ncdata,'primf',start = c(1,1,1131)) #get primf from 1980 to 2017
secdf<- ncvar_get(ncdata,'secdf',start = c(1,1,1131))
c3ann<- ncvar_get(ncdata,'c3ann',start = c(1,1,1131))
c4ann<- ncvar_get(ncdata,'c4ann',start = c(1,1,1131))
#calculate the slope and p value
primf<-primf[,,1:37] #取1980年到2016年37年的資料
secdf<-secdf[,,1:37]
c3ann<-c3ann[,,1:37]
c4ann<-c4ann[,,1:37]
is.array(c3ann)
forest<-array(NA,dim=c(1440,720,37))
cropland<-array(NA,dim=c(1440,720,37))
forest<-primf+secdf
cropland<-c3ann+c4ann
vec_forest<- apply(forest, 3, function(x) as.vector(x))
vec_cropland<- apply(cropland, 3, function(x) as.vector(x))
dim(vec_forest)
dim(vec_cropland)
#計算趨勢V和P-value值的函式
Slope<- function(x) ifelse(sum(!is.na(x)) < 5, NA, sens.slope(na.omit(x))$estimates)
Sign <- function(x) ifelse(sum(!is.na(x)) < 10, NA, sens.slope(na.omit(x))$p.value)
Trends_forest <- apply(vec_forest, 1, Slope)
Trends_cropland <- apply(vec_cropland, 1, Slope)
Pvalue_forest <- apply(vec_forest, 1, Sign)
Pvalue_cropland <- apply(vec_cropland, 1, Sign)
#最後的單位應該是100%,所以乘以100
Sen_forest<- data.frame(Slope = Trends_forest*100, Pvalue = Pvalue_forest)
Sen_cropland<-data.frame(Slope = Trends_cropland*100, Pvalue = Pvalue_cropland)
#這裡檢視最大最小值是為了畫圖的時候封頂
max(na.omit(Sen_forest$Slope))
min(na.omit(Sen_forest$Slope))
max(na.omit(Sen_cropland$Slope))
min(na.omit(Sen_cropland$Slope))
三、圖形繪製
#繪製網格
grid <- expand.grid(y = lon, x = lat)
P1_0.05 <- Sen_forest$Pvalue
P1_0.05[P1_0.05 > 0.05] <- NA
z1 <- Sen_forest$Slope
z1[z1 > 1.2] <- 1.8
z1[z1< -1.2] <- -1.8
P2_0.05 <- Sen_cropland$Pvalue
P2_0.05[P2_0.05 > 0.05] <- NA
z2 <- Sen_cropland$Slope
z2[z2 > 1.2] <- 1.8
z2[z2< -1.2] <- -1.8
#Points_del <- rep(c(1, rep(NA, 9)), length(P1_0.05)/10)
grid$Slope_forest<- z1
grid$Pvalue_forest <- P1_0.05
grid$slope_cropland<-z2
grid$Pvalue_cropland<-P2_0.05
rwg.colors <- colorRampPalette(c(brewer.pal(9,'GnBu')[9:5],brewer.pal(9,'OrRd')[c(5:9)]))
x_sel <- (grid$x%%1 == 0.875 & ifelse(floor(grid$x) %%2 == 0, TRUE, FALSE))
y_sel <- (grid$y%%1 == 0.875 & ifelse(floor(grid$y) %%2 == 0, TRUE, FALSE))
grid2 <- grid[x_sel & y_sel & !is.na(grid$Pvalue_forest),]
grid3 <- grid[x_sel & y_sel & !is.na(grid$Pvalue_cropland),]
P1 <-
ggplot()+
geom_raster(data = grid, aes(y, x,fill = Slope_forest))+
#geom_point(data = grid2, aes(y, x, fill = Pvalue_forest), size = 0.1, shape = 16)+
scale_fill_gradientn(colours = c('green', '#00A08A', 'lightgray','white','#FF0000','red','red3'), na.value = 'transparent')+ # gray50
ylim(-60,90)+ xlim(-170,180)+
coord_fixed()+
guides(fill = guide_colorbar(barwidth = 0.5, barheight = 17), title="Slope")+
theme(panel.background = element_rect(fill = 'transparent',color = 'black'), #gray50
axis.title = element_blank(),
axis.text = element_text(size = rel(1),face = "bold"),
strip.text = element_text(size = rel(1),face = "bold"),
legend.text = element_text(size = rel(1),face = "bold"),
legend.title= element_blank())
P1
P2 <-
ggplot()+
geom_raster(data = grid, aes(y, x,fill = slope_cropland))+
#geom_point(data = grid3, aes(y, x, fill = Pvalue_cropland), size = 0.1, shape = 16)+
scale_fill_gradientn(colours = c('green4','green2', 'green', 'lightgreen','gray80','gray95','#FF0000','orangered','red'), na.value = 'transparent')+
ylim(-60,90)+ xlim(-170,180)+
coord_fixed()+
guides(fill = guide_colorbar(barwidth = 0.5, barheight = 17), title="Slope")+
theme(panel.background = element_rect(fill = 'transparent',color = 'black'), #gray50
axis.title = element_blank(),
axis.text = element_text(size = rel(1),face = "bold"),
strip.text = element_text(size = rel(1),face = "bold"),
legend.text = element_text(size = rel(1),face = "bold"),
legend.title= element_blank())
P2
#---------------------------------- 1b. Summary the percentage with Sign.
Trends1 <- Sen_forest$Slope
Pvalue1 <- Sen_forest$Pvalue
y <- c( mean(Trends1 <0 & Pvalue1 < 0.05, na.rm = T),
mean(Trends1 < 0 & Pvalue1 > 0.05, na.rm = T),
mean(Trends1 > 0 & Pvalue1 >0.05, na.rm = T),
mean(Trends1 > 0 & Pvalue1 <0.05, na.rm = T))
# Name <- c("Sinificant decrease", "Decrease", "increase", "Sinificant increase")
Name <- c("- -", "-", "+", "+ +")
Name <- c("SD", "D", "I", "SI")
Perc <- data.frame(Name, y = y*100)
Perc$Name <- factor(Perc$Name, levels = Name, ordered = T)
label <- paste(Name, '(',scales::percent(round(y/100,2)),')', sep='')
Trends2 <- Sen_cropland$Slope
Pvalue2 <- Sen_cropland$Pvalue
y2 <- c( mean(Trends2 < 0 & Pvalue1 < 0.05, na.rm = T),
mean(Trends2 < 0 & Pvalue1 > 0.05, na.rm = T),
mean(Trends2 > 0 & Pvalue1 > 0.05, na.rm = T),
mean(Trends2 > 0 & Pvalue1 < 0.05, na.rm = T))
Perc2 <- data.frame(Name, y2 = y2*100)
Perc2$Name <- factor(Perc2$Name, levels = Name, ordered = T)
label2 <- paste(Name, '(',scales::percent(round(y2/100,2)),')', sep='')
mycolor = colorRampPalette(brewer.pal(11,'RdYlGn'))(39)
windowsFonts(Arial=windowsFont('Arial'))
P11<-
ggplot(Perc, aes(x = Name, y = y, fill = Name))+
coord_fixed(ratio = 0.06)+
geom_bar(stat = "identity", width = 0.45, position = 'dodge')+
ylab('Percentages')+xlab('')+
scale_fill_manual(values = c('green', '#00A08A', '#FF0000', 'red'))+
scale_y_continuous(limits = c(0,50),expand = c(0,0))+
theme_cowplot()+
guides(fill = FALSE)+
theme(axis.title = element_text(size = rel(0.8), face = "bold"),
axis.text = element_text(size = rel(0.7),face = "bold"))
P22<-
ggplot(Perc2, aes(x = Name, y = y2, fill = Name))+
coord_fixed(ratio = 0.06)+
geom_bar(stat = "identity", width = 0.45, position = 'dodge')+
ylab('Percentages')+xlab('')+
scale_fill_manual(values = c('green4', 'green', '#FF0000', 'red'))+
scale_y_continuous(limits = c(0,50),expand = c(0,0))+
theme_cowplot()+
guides(fill = FALSE)+
theme(axis.title = element_text(size = rel(0.8), face = "bold"),
axis.text = element_text(size = rel(0.7),face = "bold"))
P22
#**************************************??????*********************************
fig1<- ggdraw()+
draw_plot(P1 + theme(legend.justification = "bottom"),0,0,1,1)+
draw_plot(P11+ theme(legend.justification = "top"), x = 0.02, y = -0.03,
width = 0.25, height = 0.67, scale = 0.8)
fig2<- ggdraw()+
draw_plot(P2 + theme(legend.justification = "bottom"),0,0,1,1)+
draw_plot(P22+ theme(legend.justification = "top"), x = 0.02, y = -0.03,
width = 0.25, height = 0.67, scale = 0.8)
library(patchwork)
png('E:/rroom/1980-2016 forest and cropland trend.png',
width = 9, height = 11, res = 300, units = 'in')
fig1+ fig2+ plot_layout(nrow = 2,byrow = TRUE)+
plot_annotation(tag_prefix = '(', tag_levels = 'a', tag_suffix = ')')
dev.off()
最後的圖類似這樣:
相關文章
- 【R語言】繪製權重直方圖R語言直方圖
- R語言之視覺化①②熱圖繪製2R語言視覺化
- 用c語言處理檔案C語言
- Go 語言處理 yaml 檔案GoYAML
- ACL 2019全程回顧:自然語言處理趨勢自然語言處理
- js讀取excel檔案,繪製echarts圖形---資料處理JSExcelEcharts
- 繪製三元圖、顏色空間圖:R語言程式碼R語言
- R語言基於表格檔案的資料繪製具有多個系列的柱狀圖與直方圖R語言直方圖
- amCharts繪製帶趨勢線折線圖
- 如何使用R語言在SAP Analytics Cloud裡繪製各種統計圖表R語言Cloud
- Go語言複製檔案Go
- R語言 - 讀取CSV檔案報錯R語言
- c語言,批次處理檔案,進行gzip壓縮C語言
- E-R圖繪製例項
- R語言ggsurvplot繪製生存曲線報錯 : object of type ‘symbol‘ is not subsettableR語言ObjectSymbol
- R語言:畫樹圖R語言
- R語言中ggplot繪圖繪製L型圖形,並設定框線的粗細R語言繪圖
- 檔案複製(Go語言實現)Go
- ln 覆蓋普通檔案或目錄
- [20241013]sqlplus spool與檔案覆蓋.txtSQL
- R統計繪圖 - 熱圖簡化繪圖
- R語言中繪圖設定不輸出繪圖內容R語言繪圖
- 自然語言處理(NLP)路線圖 - kdnuggets自然語言處理
- golang寫入檔案時,覆蓋前檔案(將前檔案清空)Golang
- 檔案數字化是檔案管理的未來趨勢
- 阿里巴巴達摩院:自然語言處理技術有哪些進展和趨勢?阿里自然語言處理
- vscode ctrl+單擊覆蓋原檔案VSCode
- 介面自動化覆蓋的功能,但是導致漏測瞭如何處理
- js如何讀取excel檔案,繪製echarts圖形。JSExcelEcharts
- 使用go語言對csv檔案進行解析處理,匯入匯出。Go
- LC 氣溫變化趨勢
- r語言R語言
- 覆蓋40種語言:谷歌釋出多語言、多工NLP新基準XTREME谷歌REM
- 入門自然語言處理必看:圖解詞向量自然語言處理圖解
- 【R語言入門】R語言環境搭建R語言
- [Python影象處理] 十一.灰度直方圖概念及OpenCV繪製直方圖Python直方圖OpenCV
- 【Go語言繪圖】圖片新增文字(二)Go繪圖
- 【Go語言繪圖】圖片新增文字(一)Go繪圖