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

Bede-Fazekas Ákos b|@|ev||@t @end|ng |rom gm@||@com
Sat Jun 1 17:46:50 CEST 2019


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
>



More information about the R-sig-Geo mailing list