使用R中的NetCDF文件绘制时间 - 温度图

时间:2016-06-26 12:15:57

标签: r plot netcdf

我的总体目标是创建一个图表,其中温度是根据时间绘制的。 我为我的研究区域下载了网格化气候数据(NetCDF文件),包含每个网格单元的长,纬度,温度值和时间。我使用RNetCDF在R中导入了文件。这就是我用于此的代码:

Grid_Temp <- "FULL_TMP_cru_ts3.23.1901.2014.tmp.dat_65-80E_35-45N.nc"
fid_T <- open.nc (Grid_Temp)
print.nc(fid_T)
Temp <- read.nc(fid_T)
close.nc(fid_T)

结果,我得到一个包含4个元素的列表:time(1d),lon(1D),lat(1D)和tmp(3D:Long,Lat,Time)。

我不知道如何使用此输出来生成我的情节。 首先,我想限制时间元素。目前时间是从1901年至2014年。但是,我需要从1930年开始。时间以月为单位,意味着时间元素包含0:1368之间的数字,因为月份是连续编号的。我需要从360号开始。任何人都可以帮助我,我如何限制我的时间?

对于情节:我需要x轴上的时间(1930-2014),y轴应该是研究区域的平均温度。我现在不知道如何处理这种清单,因此,我不知道如何创建这种情节。

非常感谢每一个答案! 我正在为我的硕士论文做这个,我是R的初学者,所以我感谢各种帮助和提示! 谢谢

1 个答案:

答案 0 :(得分:0)

尝试切换到ncdf4 pkg并查看是否有帮助:

library(ncdf4)
library(ggplot2)
library(dplyr)

# open the HadCRUT temp netcdf
had <- nc_open("HadCRUT.4.4.0.0.median.nc")

print(had) # not shown

# get the data

lon <- ncvar_get(had, "longitude")
lat <- ncvar_get(had, "latitude")
time <- ncvar_get(had, "time")
temperature_anomaly <- ncvar_get(had, "temperature_anomaly")

nc_close(had) # done with the file

# ref origin for this one is diff than 
time <- as.Date(time, origin="1850-01-01")

# pick a lat/lon pair

(lon[10])
## [1] -132.5

(lat[10])
## [1] -42.5

data_frame(month=time,
           temp=temperature_anomaly[10,10,],
           col=ifelse(temp<0, "#b2182b", "#2166ac")) %>% 
  ggplot(aes(month, temp)) +
  geom_point(aes(color=col), size=0.15) +
  scale_color_identity() +
  theme_bw()