首页 > 解决方案 > 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 上也有。我应用了许多建议的解决方案,其中大多数都使用rasterandsf包,但结果从来不是我想要的。

这里有谁知道如何做到这一点?

标签: rggplot2netcdfr-rasternetcdf4

解决方案


推荐阅读