[R-sig-Geo] Creating a median curve among multiple curvesin ggplot

r@i@1290 m@iii@g oii @im@com r@i@1290 m@iii@g oii @im@com
Sat Jun 1 23:44:59 CEST 2019


Hi Akos,

Yes, I decided to experiment with different orders, just to see what would happen, and, indeed, the values returned were exactly the same. This confirms the automatic functionality!
Many thanks, once again! Your help was extremely appreciated!!
Have yourself a good weekend, as well.

-----Original Message-----
From: Bede-Fazekas Ákos <bfalevlist using gmail.com>
To: rain1290 <rain1290 using aim.com>; r-sig-geo <r-sig-geo using r-project.org>
Sent: Sat, Jun 1, 2019 4:04 pm
Subject: Re: [R-sig-Geo] Creating a median curve among multiple curvesin ggplot

 Hi,
 median() automatically sorts the data, so the order of the rasters is not important, I think.
 Have a nice weekend,
 Ákos
 
 2019.06.01. 19:23 keltezéssel, rain1290 using aim.com írta:
  
 
Hi Akos,
 
 Actually, I believe both of these worked just fine after testing them and returned the 140 values in each case! For the second variable, I first received an error after running it, but this was because the number of years/layers differed slightly for one of the objects (Model42), being 138 years/layers. So, I just modified the code to account for this (i.e. 1:138), and it worked fine. :)   
  Also, I need not arrange the raster objects in ascending order, correct? My sense is that it automatically sorts the values for each object from lowest to highest for each year for each grid cell. At least that is what it did for the 1-dimensional objects for the first variable, as the median values shown there are correctly ascending (as they should be with each year).  
  Many, many thanks, once again! 
  -----Original Message-----
 From: Bede-Fazekas Ákos <bfalevlist using gmail.com>
 To: rain1290 <rain1290 using aim.com>; r-sig-geo <r-sig-geo using r-project.org>
 Sent: Sat, Jun 1, 2019 12:47 pm
 Subject: Re: [R-sig-Geo] Creating a median curve among multiple curvesin ggplot
 
   Hello,
 Sorry for this, it was wrong. This might work:
 RCP1pctCO2Median <- apply(X = cbind(get, IPSL, IPSLMR, IPSL5, MIROC, HadGEM, MPI, MPI5, GFDL, GFDL5), MARGIN = 1, FUN = median)
 
 OK, I understand now the structure of your rasters.
 Something like this may do the task:
 stack(lapply(X = 1:140, FUN = function(year) {calc(x = stack(lapply(X = list(Model2, Model10, Model18, Model26, subset14, Model42, subset20, subset24, Model60, Model68), FUN = "[[", i = year)), fun = median)}))
 (Again, this is not tested. If you send us reproducible examples then we can try the code what we suggest you.)
 HTH,
 Ákos
 
 2019.06.01. 18:06 keltezéssel, rain1290 using aim.com írta:
  
 
     Hi Akos (and everyone), 
  Thank you kindly for this response! 
 I tried your suggestions, but for the first variable:  
  RCP1pctCO2Median <- median(apply(X = cbind(get, IPSL, IPSLMR, IPSL5, 
  MIROC, HadGEM, MPI, MPI5, GFDL, GFDL5)), MARGIN = 1, FUN = median) 
  I receive this error: 
   Error in match.fun(FUN) : argument "FUN" is missing, with no default 
  Why would that appear? 
  Also, for the raster objects for the second variable, yes, what you suggested worked, but this returns only the single median value per grid cell for the 140 years, as opposed to 140 medians (i.e. a median for each year/each layer) for each grid cell. Ideally, I would like to derive 140 medians corresponding to each of the 140 years/layers for each grid cell. Is there a way to do this?  
  Here is what some of the values/structure look like for the object Model2: 
   > head(Model2)
             X1       X2        X3        X4       X5       X6        X7        X8        X9       X10       X11
 [1,]  7.395703 6.493830  7.432156  6.767403 11.66004 7.158040 13.968703  8.139962  7.927880 10.367045  8.560315
 [2,]  7.277671 6.318810  8.406366  5.974478 12.51484 7.405914 15.350679  7.977879  8.039850  9.696597  9.374163
 [3,]  6.919258 6.119063  9.485675  6.256432 13.44547 7.542754 10.846225  8.441847  8.155234 10.129576 10.074353
 [4,]  6.633444 6.300559 10.349174  6.733875 14.35567 7.589797 12.452223  8.384633  8.697707 10.025741 10.589373
 [5,]  6.986749 7.057785 11.202586  8.037964 15.08123 7.668468 11.838186  8.506311  8.978721  9.699996 10.256382
 [6,]  7.287383 7.561100 12.091483  8.384658 15.56648 7.662876  8.925544  9.431210  8.979276 10.047477 10.898319
 [7,]  7.957773 7.732338 13.998432  8.982379 15.50928 7.599275  8.548879  8.287948  8.745722  9.496011 10.861937
            X12       X13       X14       X15      X16       X17      X18       X19      X20       X21       X22
 [1,]  5.533977 10.534669 12.136978  8.897428 10.07571  8.485538 7.731084 10.557160 15.36603 12.320665 11.274374
 [2,]  6.010256 10.299062 12.703854  9.522287 11.08078  8.996936 6.919732 10.900610 14.44405 11.949420 11.202058
 [3,]  6.590125  9.553597 12.781633  9.015289 11.35786  9.215069 6.888467  9.571572 13.47964 10.308230  9.772387
 [4,]  7.042177  8.800974 12.292124 11.457335 11.96268  9.329095 7.078347  9.072567 13.90133  9.780438  9.901325
 [5,]  7.613796  9.617242 11.511468 11.527631 12.94864  9.510409 7.504535  8.380163 14.01753  9.804405 10.389509
 [6,]  8.146129  9.211740 11.250921 11.483143 13.93948  8.881543 7.888670  8.917588 13.32526  8.609215  9.584101
 [7,]  8.551682  8.858268 10.417808 11.292067 15.19191  9.224810 8.231571  7.949825 12.58279  8.042569  9.816563 
  Thanks, once again, and I look forward to your response! 
  -----Original Message-----
 From: Bede-Fazekas Ákos <bfalevlist using gmail.com>
 To: r-sig-geo <r-sig-geo using r-project.org>
 Sent: Sat, Jun 1, 2019 11:47 am
 Subject: Re: [R-sig-Geo] Creating a median curve among multiple curvesin ggplot
 
 Hello,
 in case of the vectors, you can get the median of the different models 
 for each of the years using apply() function:
 RCP1pctCO2Median <- median(apply(X = cbind(get, IPSL, IPSLMR, IPSL5, 
 MIROC, HadGEM, MPI, MPI5, GFDL, GFDL5)), MARGIN = 1, FUN = median)
 It calculates the median of each of the rows (= years) of the matrix.
 
 In case of rasters, use raster::calc(), if all the rasters are 
 single-layer RasterLayer objects:
 RCP1pctCO2ModelMedian <- calc(x = stack(Model2, Model10, Model18, 
 Model26, subset14, Model42, subset20, subset24, Model60, Model68), fun = 
 median)
 Since your rasters are multi-layer RasterBrick, you should give more 
 details of their stucture/content to let us answer your question.
 
 HTH,
 Ákos Bede-Fazekas
 Hungarian Academy of Sciences
 
 2019.06.01. 17:24 keltezéssel, rain1290--- via R-sig-Geo írta:
 > Hi there,
 > Using ggplot, I have successfully created a plot that contains 37 separate line plots that each represent a trend in precipitation over 140 years. However, what I would like to do is show the curve that is the "median" of these lines. Is this possible to do?
 > Here is what I have so far in an attempt to do this, but I am not certain if it is the correct approach to create the desired median line - the first variable shows x-variable objects (each 1-dimensional) and the second variable shows the y-variable objects (3-dimensional):
 > RCP1pctCO2Median <- median(cbind(get, IPSL, IPSLMR, IPSL5, MIROC, HadGEM, MPI, MPI5, GFDL, GFDL5))
 > RCP1pctCO2ModelMedian <- median(cbind(Model2, Model10, Model18, Model26, subset14, Model42, subset20, subset24, Model60, Model68))
 >
 > The first variable appears to work, but it only returns one value, and I want all of the 140 values corresponding to whichever curve was returned. Why is it that only one value returns? Here is what the variable contains:
 >> RCP1pctCO2Median[1] 1.189044
 > When I run the second variable the same way, I receive the following error:
 > Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
 >    'x' must be atomic
 > Why does this appear?  I tried "unlist" and "as.vector", but the same error appears.
 > If it helps, to show what the 1-dimensional objects of the first variable look like, here is an example of one (the object "get"):
 >> get
 >    [1] 0.00000000 0.00644777 0.01250601 0.01746194 0.02656210 0.03360072 0.04399755 0.05404601 0.06154399 0.07105423
 >  [11] 0.08315538 0.08925389 0.10060635 0.10833009 0.12196157 0.13730928 0.14890850 0.16087072 0.17361501 0.18310140
 >  [21] 0.19810085 0.21316248 0.22423607 0.23529990 0.24695577 0.26119599 0.27643532 0.28517213 0.29978970 0.31870887
 >  [31] 0.33418021 0.34585044 0.36071229 0.37379062 0.39324498 0.41099992 0.42437476 0.43643925 0.44687960 0.45864752
 >  [41] 0.48055932 0.49634662 0.50779009 0.52861303 0.54971600 0.56138206 0.57472491 0.59202033 0.60975420 0.63057494
 >  [51] 0.64653736 0.66088217 0.68168259 0.69649595 0.71363837 0.72988760 0.75010443 0.76776582 0.78613955 0.81099886
 >  [61] 0.82979667 0.84431553 0.86824787 0.88447672 0.89909577 0.92162514 0.94854647 0.96631038 0.98120815 1.00357985
 >  [71] 1.02769291 1.04733002 1.06585240 1.09083056 1.11592567 1.13627303 1.15866983 1.17941856 1.19748724 1.22012913
 >  [81] 1.24142718 1.26937973 1.29283488 1.31011736 1.33134723 1.36014366 1.38395822 1.40615380 1.42895305 1.45046842
 >  [91] 1.47504234 1.49920797 1.52142668 1.54559219 1.57354927 1.60000896 1.62449229 1.64506662 1.67100036 1.70185161
 > [101] 1.72964287 1.75235510 1.77455521 1.80183053 1.83121216 1.85619974 1.88188243 1.91002214 1.93333006 1.96133912
 > [111] 1.98944890 2.01225924 2.03804183 2.06614304 2.09558916 2.12400699 2.14846969 2.18011498 2.20532560 2.23031592
 > [121] 2.26106715 2.28796983 2.31954741 2.35274339 2.37794161 2.40643740 2.43963051 2.46948838 2.49733877 2.53035021
 > [131] 2.56229091 2.59265327 2.61934137 2.64592481 2.67754769 2.70779777 2.73824191 2.76880598 2.80077982 2.83057237
 >
 > For the 3-D objects within the second variable, here is what is contained in the object "Model2", for example:
 >> Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var = "onedaymax")
 >> Model2class      : 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 : C:/Users/Travis/Documents/Other documents/All netCDF files/netcdffiles/MaxPrecCCCMACanESM21pctCO2.nc
 > names      : X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ...
 > z-value    : 1, 140 (min, max)
 > varname    : onedaymax
 >
 >
 > In any case, I am not even certain if what I have done above is the correct approach altogether to create a median line, but I would greatly appreciate any assistance! Your feedback would be extremely valuable to me!
 > Thank you, and I very much look forward to hearing from you! 
 >     [[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
 >
 
 _______________________________________________
 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