[英]Why is the data is not plotting on top of global map in R?
我正在嘗試 plot 來自世界海洋地圖集( https://www.nodc.noaa.gov/cgi-bin/OC5/woa18/woa18.pl )的海面溫度數據的年度和年代際差異與 netcdf文件。
我設法創建了一個全局 map 但我無法將年度數據疊加到它上面。 我的代碼中是否缺少某些內容? 是否可以沿 x 和 y 軸添加緯度和經度? 我可以用 1955 - 64 年代數據減去 2005 - 2017 年代數據還是需要對數據做些什么? 謝謝!
remove(list=ls())
library(rgdal)
library(raster)
library(rgeos)
library(palr)
library(rworldmap)
library(reshape2)
library(raadtools)
library(raadfiles)
library(sp)
library(spbabel)
library(dplyr)
datafile <- "/Users/Desktop/woa18_decav_t00_01.nc"
path_split <- stringr::str_split(basename(datafile), ".")
img_basename <- basename(datafile)
img_startdate_string <- substr(img_basename, 2,8)
image_startdate <- as.Date(strptime(img_startdate_string, "%Y%j"))
pprj <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0, 0, 0"
target_extent = c(-180, 180, -90, 90)
g4 <- rgeos::gBuffer(SpatialPoints(cbind(0, 0), proj4string = CRS(pprj)),
width = spDists(rbind(c(-180, -90), c(-180, -90)), longlat = TRUE, segments = T) * 1000,
quadsegs = 180)
data("countriesLow", package = "rworldmap")
sptable(countriesLow) <- subset(sptable(countriesLow), y_ > -84)
w <- crop(rgeos::gBuffer(spTransform(countriesLow, CRS(pprj)), width = 0), g4)
pal <- palr::sstPal(palette = TRUE)
target <- raster(g4)
res(target) <- c(25000, 25000)
sst <- projectRaster(raster(datafile), target)
sst <- mask(sst, g4)
#png("/Users/Desktop/plot1.png", width = 300, height = 300, units = "mm", res = 400)
par(mar=c(6,6,4,4))
plot(w, col="grey", breaks = pal$breaks, asp = 1, cex.axis=1.5, cex.lab=1.5, xlim = c(xmin(w), xmax(w)), ylim = c(ymin(w), ymax(w)))
mtext(text = image_startdate, side = 3, line = 0) #need this to state the decadal difference
image(chl, col = pal$cols[-1], breaks = pal$breaks, add = TRUE)
plot(g4, add=TRUE)
plot(w, col="grey", add=TRUE, breaks = pal$breaks, asp = 1, cex.axis=1.5, cex.lab=1.5, xlim = c(xmin(w), xmax(w)), ylim = c(ymin(w), ymax(w)))
#dev.off()
這段代碼對我有用
nc_file <- "https://data.nodc.noaa.gov/thredds/dodsC/ncei/woa/temperature/decav/1.00/woa18_decav_t01_01.nc"
library( ncdf4 )
library( raster )
library(maptools)
#read nc-file
ncData <- nc_open( nc_file )
#get longitude, latitude and temperature
lon <- ncvar_get( ncData, "lon" )
lat <- ncvar_get( ncData, "lat" )
temp <- ncvar_get( ncData, "t_an")
#check
dim( temp )
#[1] 360 180 57 lon, lat, depth
#depth[1] = surface, so we take that slice
surface_slice <- temp[, , 1]
#close connections
nc_close( ncData )
#create raster
r <- raster( t( surface_slice ),
xmn = min(lon),
xmx = max(lon),
ymn = min(lat),
ymx = max(lat),
crs = CRS( "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0" ) )
r <- flip(r, direction='y')
plot(r)
#add countries
data(wrld_simpl)
plot(wrld_simpl, add = TRUE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.