R中轨迹数据的NetCDF显示

问题描述 投票:0回答:1

我正在尝试显示以下经典NetCDF数据Data Link Click Here。此数据有38个变量轨迹数据,我正尝试提取其中之一“空气温度”。虽然我能够提取数据,但不幸的是我无法使用图在R中显示数据。显示应类似于下图enter image description here,我以全图方式绘制。另一个问题是,由于轨迹不同,我无法堆叠所有数据。有什么办法可以显示和堆叠一个变量的所有文件。

> library(ncdf4)
> library(rasterVis)
> library(raster)
lon <- ncvar_get(ncin, "lon")
lat <- ncvar_get(ncin, "lat")
data <-lon <- ncvar_get(ncin, "air_temp_ac")          #to extract variable
> dim(data)
[1] 6639
> dput(data)
structure(c(NA, 15, 14, 13, 11, NA, 11, 11, 11, 11, 11, NA, 14, 
14, 12, NA, 12, 14, 14, 14, 16, 21, 25, 27, 26, 19, 21, 22, 22, 
21, 22, 22, 23, 24, 22, 23, 25, 23, 26, 25, 25, 23, 24, 24, 25, 
28, 28, 29, 29, 28, 28, 26, 27, 27, 29, 31, 31, 34, 36, 37, 38, 
41, 37, 37, 39, 36, 29, 33, 38, 35, 36, 36, 36, 37, 37, 36, 37, 
34, 33, 34, 38, 37, 37, 37, 37, 37, 39, 39, 39, 40, 39, 39, 40,
> plot(lon,lat,data)
Error in plot.xy(xy, type, ...) : invalid plot type

我尝试过

> r <- raster(t(data), 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)

但是,情节超出范围enter image description here

r plot raster netcdf
1个回答
0
投票

这里是使用ggplot2sfrnaturalearth的解决方案。使用您的代码,我无法从nc文件中提取变量“ air_temp_ac”。实际变量是“ air_temp_AC”(请注意,这是区分大小写的)

``` r
library(ncdf4)
library(ggplot2)
library(rnaturalearth) 
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

world <- ne_countries(scale = "medium", returnclass = "sf")

ncin  <- nc_open("~/nc.nc")

df    <- data.frame(lon    = ncvar_get(ncin, "lon"), 
                    lat    = ncvar_get(ncin, "lat"),
                    kelvin = ncvar_get(ncin, "air_temp_AC"))

ggplot(data = world) +
  geom_sf() +
  coord_sf(xlim = c(0, 90), ylim = c(0, 55), expand = FALSE) +
  geom_point(data = df, aes(x = lon, y = lat, colour = kelvin)) +
  scale_color_gradientn( colours = c("lightblue", "blue", "yellow", "red"))

“”

reprex package(v0.3.0)在2020-02-23创建

© www.soinside.com 2019 - 2024. All rights reserved.