使用R

时间:2019-03-26 18:17:26

标签: r plot ncdf4

我目前正在尝试使用与线有关的图来绘制降水数据(y轴值)和使用R的累积排放数据(x轴)。这两个数据都在两个单独的netCDF文件中找到,我我已经读过R。最终,我想做的是将选定位置的降水量与累积排放量的关系作图(如下代码所示)。到目前为止,我已经使用以下代码(使用#突出显示每个步骤):

library(raster)
library(ncdf4)
library(maps)
library(maptools)
library(rasterVis)
library(ggplot2)
library(rgdal)
library(sp)

#Geting cumulative emissions data for x-axis

ncfname <- "cumulative_emissions_1pctCO2.nc"
Model1 <- nc_open(ncfname)
print(Model1)
get <- ncvar_get(Model1, "cum_co2_emi-CanESM2") #units of terratones ofcarbon (TtC) for x-axis
print(get)
Year <- ncvar_get(Model1, "time") #140 years


#Getting Model data for extreme precipitation (units of millimeters/day)for y-axis

ncfname1 <- "MaxPrecCCCMACanESM21pctCO2.nc"
Model2 <- nc_open(ncfname1)
print(Model2)
get1 <- ncvar_get(Model2, "onedaymax") #units of millimeters/day
print(get1)
#Reading in latitude, longitude and time from this file:
latitude <- ncvar_get(Model2, "lat") #64 degrees latitude
longitude <- ncvar_get(Model2, "lon") #128 degrees longitude
Year1 <- ncvar_get(Model2, "Year") #140 years

#Plotting attempt

r_brick <- brick(get, xmn=min(latitude), xmx=max(latitude),  
ymn=min(longitude), ymx=max(longitude), crs=CRS("+proj=longlat +ellps=WGS84  
+datum=WGS84 +no_defs+ towgs84=0,0,0"))
randompointlon <- 30 #selecting a longitude
randompointlat <- -5 #selecting a latitude
Hope <- extract(r_brick, 
SpatialPoints(cbind(randompointlon,randompointlat)),method = 'simple')
df <- data.frame(cumulativeemissions=seq(from = 1, to = 140, by = 1),   
Precipitation=t(Hope))
ggplot(data = df, aes(x = get, y = Precipitation, 
group=1))+geom_line()+ggtitle("One-day maximum precipitation (mm/day)   
for random location for CanESM2 1pctCO2 as a function of cumulative 
emissions")

print(Model1)产生以下内容(我现在读入变量#2以便使用):

文件accumulation_emissions_1pctCO2.nc(NC_FORMAT_NETCDF4):

 14 variables (excluding dimension variables):
    float cum_co2_emi-BNU-ESM[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for BNU-ESM
        units: Tt C
    float cum_co2_emi-CanESM2[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for CanESM2
        units: Tt C
    float cum_co2_emi-CESM1-BGC[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for CESM1-BGC
        units: Tt C
    float cum_co2_emi-HadGEM2-ES[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for HadGEM2-ES
        units: Tt C
    float cum_co2_emi-inmcm4[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for inmcm4
        units: Tt C
    float cum_co2_emi-IPSL-CM5A-LR[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for IPSL-CM5A-LR
        units: Tt C
    float cum_co2_emi-IPSL-CM5A-MR[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for IPSL-CM5A-MR
        units: Tt C
    float cum_co2_emi-IPSL-CM5B-LR[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for IPSL-CM5B-LR
        units: Tt C
    float cum_co2_emi-MIROC-ESM[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for MIROC-ESM
        units: Tt C
    float cum_co2_emi-MPI-ESM-LR[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for MPI-ESM-LR
        units: Tt C
    float cum_co2_emi-MPI-ESM-MR[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for MPI-ESM-MR
        units: Tt C
    float cum_co2_emi-NorESM1-ME[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for NorESM1-ME
        units: Tt C
    float cum_co2_emi-GFDL-ESM2G[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for GFDL-ESM2G
        units: Tt C
    float cum_co2_emi-GFDL-ESM2M[time]   (Contiguous storage)  
        long_name: Cumulative carbon emissions for GFDL-ESM2M
        units: Tt C

 1 dimensions:
    time  Size:140
        units: years since 0-1-1 0:0:0
        long_name: time
        standard_name: time
        calender: noleap

4 global attributes:
    description: Cumulative carbon emissions for the 1pctCO2 scenario from the CMIP5 dataset.
    history: Created Fri Jul 21 14:50:39 2017
    source: CMIP5 archieve

print(Model2)产生以下内容:

文件MaxPrecCCCMACanESM21pctCO2.nc(NC_FORMAT_NETCDF4):

 3 variables (excluding dimension variables):
    double onedaymax[lon,lat,time]   (Contiguous storage)  
        units: mm/day
    double fivedaymax[lon,lat,time]   (Contiguous storage)  
        units: mm/day
    short Year[time]   (Contiguous storage)  

 3 dimensions:
    time  Size:140
    lat  Size:64
        units: degree North
    lon  Size:128
        units: degree East

3 global attributes:
    description: Annual global maximum precipitation from the CanESM2 1pctCO2 scenario
    history: Created Mon Jun  4 11:24:02 2018
    contact: rain1290@aim.com

因此,总的来说,这就是我要实现的目标,但是我不确定在ggplot函数中执行的操作是否正确。

在此方面提供的任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您真正要求的是什么。如果与从ncdf文件中获取数据有关,则应重点关注这一点。如果它是关于ggplot的,那将提供一些简单的数据,并忽略所有ncdf的内容。另外,我也不知道什么是“与线有关的图”(也许是ggplot的东西?)。您是指散点图吗?

要获取ncdf数据,您可以执行以下操作:

library(raster)
Model1 <- brick("cumulative_emissions_1pctCO2.nc", var="cum_co2_emi-CanESM2")
Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax") 
latlon <- cbind(30, -5) 
Hope1 <- extract(Model1, lonlat)
Hope2 <- extract(Model2, lonlat)

现在,也许:

plot(Hope1, Hope2)