0

I am currently trying to plot, using a line-related plot, precipitation data (y-axis values) with cumulative emissions data (x-axis) using R. Both of these data are found on two separate netCDF files that I have already read into R. Ultimately, What I would like to do is plot precipitation as a function of cumulative emissions for a selected location (as shown below in the following code). I have, so far, used the following code (with # to highlight each step):

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) yields the following (I read in variable #2 to work with for now):

File cumulative_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) yields the following:

File 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

So, in general, this is what I am trying to achieve, but I am not sure if what I am doing in the ggplot function is the right approach.

Any assistance with this would be greatly appreciated!

Thanks

Android17
  • 93
  • 1
  • 12
  • 1
    Could you clarify exactly what the result is that you are looking for, and why the `ggplot` call is not working for you? Also, your code is difficult to read; please make sure all comments are actually commented out, (and it would be helpful to have spaces around operators like `<-` and `=`). – C. Braun Mar 26 '19 at 18:45
  • Hi C. Braun, Thank you for your reply! I just added spaced around operators. My goal is to use ggplot (or perhaps a better plotting command that I am not aware of) to place precipitation (y-axis) as a function of cumulative emissions (x-axis). I ultimately would like to see if there is link between precipitation and increasing cumulative emissions. – Android17 Mar 26 '19 at 19:05
  • @C.Braun Also, just to be clear, I am specifically trying to make a line plot to show the relationship between precipitation and cumulative emissions (so, precipitation as a function of cumulative emissions - I just added this edit to my original posting). – Android17 Mar 26 '19 at 22:14

1 Answers1

0

It is not clear what you are really asking help for. If it has to do with getting data from the ncdf files, that focus on that. If it is about ggplot, that provide some simple data and leave out all the ncdf stuff. Also, I do not know what a "line-related plot" is (perhaps a ggplot thing?). Do you mean a scatter plot?

To get ncdf data, you can do:

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)

And now, perhaps:

plot(Hope1, Hope2)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Hi Robert, Thank you for your reply. I apologize for not being clearer - I think the best way to describe the type of plot that I am trying to create for a particular location is a "line graph", like what you would see in a time series. The objective is to plot (using a line graph) precipitation as a function of cumulative emissions for, in this case, -5 degrees latitude and 30 degrees longitude. Does that make sense? Also, "Model1" does not run, I think because that file is 1-dimensional, and I think this affects Hope1. Model2 works just fine! Could that be the reason? – Android17 Mar 27 '19 at 15:02
  • Yes, if Model1 is one-dimensional, it won't work like this. If your question is about a plot, provide some example data `x=c(1,2,3)` and `y=c(4,3,6)` and take it from there. – Robert Hijmans Mar 27 '19 at 15:40
  • Hi Robert, Yes, the question pertains to plotting (making a line graph using those above two variables). The data that I would like to plot on the x-axis would come from the variable "cum_Co2_emi-CanESM2" (140 values, with a minimum value of 0 Ttc and maximum of 2.8 Ttc), while the data for the y-axis would come from the variable "onedaymax" (which contains precipitation data over 140 years/layers, with a minimum value of 0.03 mm/day and a maximum of 410.18 mm/day). – Android17 Mar 27 '19 at 16:11
  • Actually, just to add to my above reply, the minimum and maximum specified for "onedaymax" is only globally for the first year. I want those 140 values/years specifically for latitude -5 degrees and longitude 30 degrees as a function of cumulative emissions (the minimum and maximum specified above for this is fine - i.e. minimum of 0.00 TtC and a maximum of 2.80 TtC). – Android17 Mar 27 '19 at 16:34
  • Hi Robert, I just wanted to let you know that it worked, and I was able to convert the plot type into a line graph. :) Also, I'm not sure if I should create a new topic for this, but I am trying to plot another line (with a different color) on the same plot the same way. However, because the new cumulative emissions dataframe that I am using contains a different number of values (it has 95 values) than the original cumulative emissions dataframe (which has 140 values), it is giving errors when I try "lines(get2.teratons,Model4,type="o",col="blue")". The range of x and y values match for both. – Android17 Mar 28 '19 at 16:34
  • Hi Robert, Apologies for the double message, but just to elaborate on the above message, it is actually the x and y lengths that differ in the second plot that I am trying to create. For example, the new cumulative emissions dataframe has 90 values (x-axis), while the new "onedaymax" variable (y-axis) has 95 values. Is there a way to overcome this and successfully plot them? – Android17 Mar 28 '19 at 16:57