r - R - 在地图上绘制 netcdf 数据
问题描述
我正在使用 netcdf 文件,其中包含来自观察的数据或来自模型输出的数据。由于数据是网格化的,我正在寻找一种在地图上绘制它的好方法。
到目前为止,我已经使用了类似的东西来完成它:
library(ncdf4)
library(maps)
library(RColorBrewer)
library(ggplot2)
library(dplyr)
library(readr)
library(tidyr)
library(viridis)
library(weathermetrics)
library(chron)
library(lattice)
# open NetCDF-file
ncin <- nc_open("precipitation_input.nc")
# get longitutes and latitudes
lon <- ncvar_get(ncin,"longitude")
nlon <- dim(lon)
lat <- ncvar_get(ncin,"latitude", verbose=F)
nlat <- dim(lat)
# get the time variable and attributes
tori <- ncvar_get(ncin,"time") # for time original
nt <- dim(tori)
# get the variable and its attributes
rr.array <- ncvar_get(ncin,"rr") #dimensions (lon,lat,time)
# Plot
m <- 204 # chose the day number of the year
rr.slice <- rr.array[,,m]
mapCDFtemp <- function(lat,lon,tas)
{
titletext <- "Precipitation on 22nd July 2010 E-OBS"
expand.grid(lon, lat) %>%
rename(lon = Var1, lat = Var2) %>%
mutate(lon = ifelse(lon > 180, -(360 - lon), lon),
tas = as.vector(tas)) %>%
ggplot() +
geom_point(aes(x = lon, y = lat, color = tas),
size = 0.8) +
borders("world", colour="black", fill=NA) +
lims(x = c(5, 16), y = c(46, 56)) +
scale_color_viridis(na.value="white", limits= c(0,100), name = "Precipitation [mm]") +
theme(legend.direction="vertical", legend.position="right", legend.key.width=unit(0.4,"cm"), legend.key.heigh=unit(2,"cm")) +
coord_quickmap() +
ggtitle(titletext)
}
mapCDFtemp(lat, lon, rr.slice)
这对于一个简单的视图来说基本上是可以的,但它看起来并不专业。
互联网上有一些类似的问题,Stack Overflow 上也有。我应用了许多建议的解决方案,其中大多数都使用raster
andsf
包,但结果从来不是我想要的。
这里有谁知道如何做到这一点?
解决方案
推荐阅读
- c# - .NET Core 2.2:xUnit Theory Inlinedata 不适用于枚举值
- php - 在每个 foreach 中使用多个 foreach 插入仅在每个 foreach 中获取最终值
- regex - 用于在 MATLAB 中捕获科学数字的 RegEx
- c# - 如何将新数据成员添加到我的自定义复选框列表?
- sql-server - 如何让自动迁移与 SQL Server 一起工作
- javascript - 我如何显示我的登录用户下的他/她的订单列表?
- javascript - 如何修复 vue js 或 javascript 比较逻辑错误
- node.js - 我根本无法启动管理 SDK
- javascript - 如何将使用 id 的函数替换为类名
- android - react-native - 如何检测何时按下主页按钮?