[R-sig-Geo] Selecting a range of longitude and latitudes

r@i@1290 m@iii@g oii @im@com r@i@1290 m@iii@g oii @im@com
Sun Apr 21 16:31:34 CEST 2019


Hi Tom (and everyone),

Ah, yes, that is what I wanted to consider in terms of selecting grid cells! :) But let's say that I wanted to aggregate each of the values of these grid cells and plot this as a function x-axis values. The values already computed for each grid cell for the y-axis correspond to average precipitation and consist of 140 values per grid cell. The variable containing these y-axis values is called "RCP1pctCO2Mean", which is 3-dimensional and has the following attributes:
class       : RasterBrick 
dimensions  : 64, 128, 8192, 140  (nrow, ncol, ncell, nlayers)
resolution  : 2.8125, 2.789327  (x, y)
extent      : -181.4062, 178.5938, -89.25846, 89.25846  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       :   layer.1,   layer.2,   layer.3,   layer.4,   layer.5,   layer.6,   layer.7,   layer.8,   layer.9,  layer.10,  layer.11,  layer.12,  layer.13,  layer.14,  layer.15, ... 
min values  : 0.5261063, 0.5625295, 0.5301681, 0.6019284, 0.5177065, 0.6775601, 0.5336965, 0.5213086, 0.5971723, 0.5305891, 0.5514522, 0.5481200, 0.4681806, 0.5437223, 0.5988844, ... 
max values  : 113.13776, 114.50780,  94.93643, 100.35409, 102.65459, 101.80622, 104.95480,  99.17839, 104.68667, 119.48342, 100.60391, 108.91896, 104.38969,  99.35735,  99.30752, ... 

For the x-axis, I already have 140 values to be plotted. The x variable is called "RCP1pctCO2cumulative". 
I tried doing something like this to start:
>expansion <- expand.grid(103:116, 3:16)>lonlat <- extract(RCP1pctCO2Mean, expansion)
>plot(RCP1pctCO2cumulative,lonlat, type="l",col="green", lwd="3", xlab="Cumulative emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="Test model") But this yields the error:
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ>dim(lonlat)
[1] 196   140>dim(expansion)[1] 196   2
What could I be doing wrong (the lengths appear to be the same)? Ultimately, the goal would be to plot the 140 values as a function of cumulative emissions (i.e. "RCP1pctCO2Mean" as a function of "RCP1pctCO2cumulative" once each of those selected grid cells are aggregated. This works absolutely fine when just selecting "one" specific grid cell. Is this possible to do for an aggregate of grid cells?
Thanks, again! 
-----Original Message-----
From: Tom Philippi <tephilippi using gmail.com>
To: rain1290 <rain1290 using aim.com>
Cc: btupper <btupper using bigelow.org>; r-sig-geo <r-sig-geo using r-project.org>
Sent: Sun, Apr 21, 2019 12:17 am
Subject: Re: [R-sig-Geo] Selecting a range of longitude and latitudes

Trav--Are you trying to specify a grid of each of those lon values by each of those lat values?  cbind() is matching the first lon with the first lat, the second with the second, generating a diagonal of locations.  [That's also why there have to be as many lats as lons.]  To fill out the grid of each lon with each lat, look at expand.grid(103:116, 3:16)
Tom
On Thu, Apr 18, 2019 at 6:51 PM rain1290--- via R-sig-Geo <r-sig-geo using r-project.org> wrote:

Hi Ben (and everyone),

Ah, yes, you're right - using the suggestion that you specified, that error message disappeared! So, if I understand, this would take all of the grid cells within those specified longitude and latitude ranges (i.e. that entire area)? 
That said, when I try plotting this using the following:
get <- ncvar_get(Model1, "cum_co2_emi-CanESM2")   #for x-axis (140 values)
Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax")    #for y-axis (140 values)

Hope2 <- extract(Model2, lonlat)

plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="CanESM2")
I receive this error:
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ
*Note that I do not have this problem when specifying a "specific" longitude and latitude, rather than a range. Is there any reason for this?
Thanks, again
-----Original Message-----
From: Ben Tupper <btupper using bigelow.org>
To: rain1290 <rain1290 using aim.com>
Cc: r-sig-geo <r-sig-geo using r-project.org>
Sent: Thu, Apr 18, 2019 9:26 pm
Subject: Re: [R-sig-Geo] Selecting a range of longitude and latitudes

Hi,
The example you give is different than the one listed in the error message.  In your example you bind two 14-element vectors into a matrix of two columns.
cbind(103:116, 3:16)
But in the error you show the first vector is only 8 elements long...
cbind(103:110, 3:16)
When R encounters an attempt to bind columns of unequal length it will try to "recycle" the shorter vector as long as it is a multiple of the longer. In this case of binding an 8-element vector and a 14-element vector it fails.  
Note that shortening up the first vector to 7 does work since 7 is a multiple of 14...
cbind(103:109, 3:16)      [,1] [,2] [1,]  103    3 [2,]  104    4 [3,]  105    5 [4,]  106    6 [5,]  107    7 [6,]  108    8 [7,]  109    9 [8,]  103   10 [9,]  104   11[10,]  105   12[11,]  106   13[12,]  107   14[13,]  108   15[14,]  109   16

Note how the sequence 103...109 appears twice in the first column.  Recycling is nice (and handy) until you get surprised by it.
Cheers,Ben

On Apr 18, 2019, at 8:37 PM, rain1290--- via R-sig-Geo <r-sig-geo using r-project.org> wrote:
Hi there,
I am trying to specify a range of longitudes and latitudes. I tried this using the following:


lonlat <- cbind(103:116, 3:16)  #This would specify a range of longitudes and latitudes

However, I receive the following warning message:
Warning message:
In cbind(103:110, 3:16) :
  number of rows of result is not a multiple of vector length (arg 1)

When I specify the longitude and latitude of a specific location, though, it works just fine, like this:
lonlat <- cbind(103, 3)

Am I specifying the ranges of coordinates incorrectly? The warning message would suggest yes, but I do not understand why.
Thanks, and any help with this would be greatly appreciated!
~Trav.~
 [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo using r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
Ecological Forecasting: https://eco.bigelow.org/





        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo using r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list