Creating a mean line plot

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating a mean line plot

R help mailing list-2
Hi there,
I am trying to create a mean line plot that shows the mean of a series of separate line plots that correspond to two climate models. Let's first try getting the mean of two line plots. To create the separate line plots, here is what I did to set up the x and y axis variables:

####Getting cumulative emissions data for x-axis: 1-dimensional ####

#For CanESM model#

ncfname <- "cumulative_emissions_1pctCO2.nc"
Model1 <- nc_open(ncfname)
get <- ncvar_get(Model1, "cum_co2_emi-CanESM2")     #units of terratones of carbon (TtC) for x-axis (140 values)
#For IPSL LR Model#
#Getting cumulative emissions data for x-axis IPSL LR 1pctCO2 IPSL <- ncvar_get(Model1, "cum_co2_emi-IPSL-CM5A-LR")     #units of terratones of carbon (TtC) for x-axis (140 values)

############################################################################################################

#####Getting precipitation data for y-axis - these are 3-dimensional####

#For CanESM2 model#
Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax")


#For IPSL LR Model#
Model10 <- brick("MaxPrecIPSLIPSL-CM5A-LR1pctCO2.nc", var="onedaymax")
#############################################################################################################
To create plots for a specific location:
lonlat <- cbind(103,3)          #specifies a specific longitude and latitude
Hope2 <- extract(Model2,lonlat)      #CanESM2
Hope6 <- extract(Model10,lonlat)   #start IPSL CM5A LR
plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative CO2 emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="One-day maximum precipitation for random location for 1pctCO2 scenario")
lines(IPSL, Hope6, type="l", lwd="3", col="green")
#############################################################################################################
So, the idea would be to create a plot that shows the mean of these two plots. Given what I showed above, how should I go about creating the mean of these two green line plots? Would you have to get the mean of the x-values, and then obtain the mean of the y-values, and then plot these?
Thanks, and any help would be greatly appreciated!
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a mean line plot

Eric Berger
I don't have your data. Are the x-values the same in both plots?
Does this example cover the situation?

f1 <- function(x) { x^3 - 2 }
f2 <- function(x) { 2 - x^2 }

xV <- seq(from=0,to=2,length=50)
y1 <- f1(xV)
y2 <- f2(xV)
y3 <- .5*(y1+y2)
plot(x=xV,y=y1,col="blue",lwd=2,type='l',xlab="x",ylab="y")
lines(x=xV,y=y2,col="green",lwd=2)
lines(x=xV,y=y3,col="red",lwd=2)
legend("topleft",legend=c("y1","y2","mean"),col=c("blue","green","red"),lwd=rep(2,3))




On Fri, Apr 12, 2019 at 5:34 PM rain1290--- via R-help <[hidden email]>
wrote:

> Hi there,
> I am trying to create a mean line plot that shows the mean of a series of
> separate line plots that correspond to two climate models. Let's first try
> getting the mean of two line plots. To create the separate line plots, here
> is what I did to set up the x and y axis variables:
>
> ####Getting cumulative emissions data for x-axis: 1-dimensional ####
>
> #For CanESM model#
>
> ncfname <- "cumulative_emissions_1pctCO2.nc"
> Model1 <- nc_open(ncfname)
> get <- ncvar_get(Model1, "cum_co2_emi-CanESM2")     #units of terratones
> of carbon (TtC) for x-axis (140 values)
> #For IPSL LR Model#
> #Getting cumulative emissions data for x-axis IPSL LR 1pctCO2 IPSL <-
> ncvar_get(Model1, "cum_co2_emi-IPSL-CM5A-LR")     #units of terratones of
> carbon (TtC) for x-axis (140 values)
>
>
> ############################################################################################################
>
> #####Getting precipitation data for y-axis - these are 3-dimensional####
>
> #For CanESM2 model#
> Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax")
>
>
> #For IPSL LR Model#
> Model10 <- brick("MaxPrecIPSLIPSL-CM5A-LR1pctCO2.nc", var="onedaymax")
>
> #############################################################################################################
> To create plots for a specific location:
> lonlat <- cbind(103,3)          #specifies a specific longitude and
> latitude
> Hope2 <- extract(Model2,lonlat)      #CanESM2
> Hope6 <- extract(Model10,lonlat)   #start IPSL CM5A LR
> plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative CO2
> emissions (TtC)", ylab="One-day maximum precipitation (mm/day)",
> main="One-day maximum precipitation for random location for 1pctCO2
> scenario")
> lines(IPSL, Hope6, type="l", lwd="3", col="green")
>
> #############################################################################################################
> So, the idea would be to create a plot that shows the mean of these two
> plots. Given what I showed above, how should I go about creating the mean
> of these two green line plots? Would you have to get the mean of the
> x-values, and then obtain the mean of the y-values, and then plot these?
> Thanks, and any help would be greatly appreciated!
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a mean line plot

R help mailing list-2
Hi Eric,

Ah, I apologize, and thank you for your response! 
I just figured out a way to average my x-values, so at least that is solved. I will still include the data for the two variables (1-dimensional) of interest that I was trying to average, just to show what was done:
get2.teratons #(90 values)
get5.teratons #(90 values)
Here is what get2.teratons looks like (same idea for get5.teratons):
    >print(get2.teratons)
    [1] 0.4558545 0.4651129 0.4747509 0.4848242 0.4950900 0.5056109 0.5159335 
    0.5262532 0.5372275 0.5481839 0.5586787 0.5694379 0.5802970
    [14] 0.5909211 0.6015753 0.6124256 0.6237733 0.6353634 0.6467227 0.6582857
    0.6702509 0.6817027 0.6935311 0.7060161 0.7182312 0.7301909
    [27] 0.7422574 0.7544744 0.7665907 0.7786409 0.7907518 0.8032732 0.8158733
    0.8284363 0.8413905 0.8545881 0.8674711 0.8797701 0.8927392
    [40] 0.9059937 0.9189707 0.9317215 0.9438155 0.9558035 0.9673665 0.9784927
    0.9900898 1.0020388 1.0132683 1.0240023 1.0347708 1.0456077
    [53] 1.0570347 1.0682903 1.0793535 1.0901511 1.1001753 1.1101276 1.1199142
    1.1293237 1.1384669 1.1470002 1.1547341 1.1622488 1.1697549
    [66] 1.1777542 1.1857587 1.1930233 1.1999645 1.2067172 1.2132979 1.2199317  
    1.2265673 1.2328599 1.2390689 1.2446050 1.2495579 1.2546455
    [79] 1.2599212 1.2648733 1.2700068 1.2753889 1.2807509 1.2856922 1.2905927
    1.2953338 1.3000484 1.3045992 1.3091128 1.3144190 
The following worked in terms of averaging all of the elements of get2.teratons and get5.teratons:
rowMeans(cbind(get2.teratons,get5.teratons))
However, I am trying to do something similar for the values on my y-axis. So, for now, here are the two variables (3-dimensional) that I would like to average:
    subset 
    subset5
Using the print function for "subset" (same idea for subset5):    >print(subset)
    class       : RasterStack
    dimensions  : 64, 128, 8192, 90  (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
    names       : X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, 
    X15, ...     >dim(subset)
    [1]  64 128  90>dim(subset5)
    [1]  64 128  90
I tried `mean(subset,subset5)`, which works, BUT it combines the 90 layers into 1 layer. I want keep the number of layers at 90, but simply average each of the grid cell values of "subset" and "subset5" for each layer. So, for instance, I want to average the values of each grid cell of layer 1 of "subset" with the values of each grid cell of layer 1 of "subset5", and then average those values of layer 2 of "subset" with those values of layer 2 of "subset5"......all the way to layer 90. That way, I have 90 averages across all grid cells.
Here is what the data looks like for "subset":
>dput(head(subset,5))
    structure(c(11.5447145886719, 11.2479725852609, 10.0223480723798,
    11.4909216295928, 12.5930442474782, 15.0295264553279, 14.6107862703502,
    13.3623332250863, 10.4473929153755, 13.262210553512, 13.3166334126145,
    13.7211008928716, 10.594900790602, 11.7217378690839, 10.8397546224296,
    14.2727348953485, 13.6185416020453, 12.7485566306859, 11.7246472276747,
    10.6815265025944, 13.1605062168092, 12.9131189547479, 12.6493454910815,
    11.6938022430986, 11.4522186107934, 8.84930260945112, 11.5785481408238,
    12.9859233275056, 13.6702361516654, 11.863912967965, 11.6624090820551,
    12.1465771459043, 12.9789240192622, 13.5916746687144, 15.0383287109435,
    7.89674604311585, 8.14079332631081, 7.05628590658307, 6.99759456329048,
    8.06435288395733, 8.00622920505702, 7.35754533670843, 6.57949370797724,
    6.26998774241656, 6.10911303665489, 10.1576759945601, 9.83650996349752,
    10.6277788057923, 10.3647025069222, 9.38627037685364, 28.411143925041,
    27.3436004295945, 25.7670222781599, 24.1854049265385, 22.7183715440333,
    10.8529561199248, 11.1584928352386, 11.4545458462089, 11.7570801638067,
    11.6314635146409, 13.7268429156393, 12.4547378160059, 12.8433785866946,
    10.282119596377, 9.66278391424567, 6.39572446234524, 8.4569685626775,
    12.253624945879, 12.4784250743687, 13.6823802720755, 8.65540341474116,
    8.34308553021401, 8.30261853989214, 7.9798299819231, 7.96007991302758,
    13.3976918645203, 15.2056947816163, 15.3097502421588, 18.0296610575169,
    17.918016621843, 14.121591579169, 14.3091559410095, 14.7470911033452,
    15.414851764217, 15.8059203531593, 22.9126498103142, 21.5608592145145,
    19.7303873486817, 17.5689237657934, 15.4688697773963, 10.2526041911915,
    10.4463449679315, 9.85705149360001, 9.5394266070798, 9.17961853556335,
    14.064371259883, 12.626935634762, 12.1540617663413, 10.9235350973904,
    9.32216013316065, 12.3676003888249, 12.9718807060272, 14.5685050170869,
    13.8497828040272, 14.0683455392718, 8.09576804749668, 8.54510050266981,
    8.02388715092093, 8.6679536383599, 9.38348234631121, 11.6279292851686,
    11.5998465567827, 11.6469369269907, 11.6286710835993, 10.8152111526579,
    17.4072104506195, 18.9169261604548, 19.5168524980545, 19.0377978142351,
    19.5594304706901, 9.74474258255213, 10.2144323755056, 10.9722976572812,
    11.5369332488626, 12.0274581480771, 14.007618650794, 14.0536692459136,
    14.4861201290041, 14.133819937706, 13.045089924708, 19.9330265633762,
    20.3158976510167, 21.4452845044434, 19.9475897010416, 20.3566399868578,
    15.703826257959, 14.8260951507837, 14.6203982178122, 14.0476305037737,
    13.2086589932442, 6.5044054761529, 6.51829722337425, 6.59741191193461,
    6.57343484926969, 7.07112564705312, 8.42645864468068, 9.15604883339256,
    10.8542435802519, 8.57339131180197, 7.89698304142803, 10.6029914226383,
    9.90388663485646, 8.46301421988755, 12.9162973724306, 9.06370310112834,
    9.92726711556315, 11.5754703059793, 8.74886247329414, 8.99941809475422,
    9.90840594749898, 11.1468604300171, 11.1322306562215, 10.49438144546,
    9.50155213940889, 8.31737467087805, 5.76932597905397, 6.14411209244281,
    7.39980584476143, 8.47632132936269, 8.00714262295514, 8.64454926922917,
    7.79559868387878, 7.14818593114614, 7.42282171268016, 9.04718739911914,
    12.0141573250294, 11.0411503817886, 11.7892528418452, 11.2668004352599,
    10.5345542309806, 14.2355003859848, 12.4114783946425, 13.1144292186946,
    14.3049817532301, 14.7282858844846, 9.90791183430701, 10.4058899218217,
    12.0624131988734, 13.2521220948547, 13.9345653355122, 12.5256763771176,
    12.3285478446633, 11.9927407242358, 11.6441268939525, 11.6448875516653,
    30.5602320469916, 30.6964941322803, 27.3358505219221, 27.5474566966295,
    24.3847575969994, 15.1250814087689, 15.0272130500525, 14.9795342702419,
    14.2658210825175, 13.437497522682, 10.7001833617687, 10.0823557935655,
    10.1298170629889, 9.99525294173509, 10.6919908896089, 9.04134479351342,
    9.57930330187082, 9.58402880933136, 8.82056106347591, 9.06912200152874,
    11.0435656271875, 12.827942892909, 14.6962288767099, 15.984565531835,
    16.3673574104905, 17.7882182411849, 17.1887206379324, 16.4347139652818,
    15.4833788517863, 14.3649869598448, 10.0324214436114, 10.9937381464988,
    10.7803415972739, 10.64134365879, 10.3700830601156, 10.7242427766323,
    10.1225153775886, 9.59254063200206, 9.67734202276915, 9.9705743137747,
    6.15209711249918, 7.6417050557211, 9.55170588567853, 12.123644258827,
    14.6793850231916, 13.8236853294075, 14.3564789090306, 13.6828002054244,
    13.0476749036461, 12.3909330926836, 12.5938401091844, 12.5098232645541,
    12.4792913440615, 10.5595408938825, 10.0890464382246, 9.20089432038367,
    8.92592284362763, 8.59467086847872, 9.42603517323732, 10.0353622343391,
    11.7311725392938, 12.4379832297564, 12.9343897104263, 12.9055073484778,
    10.8944955747575, 13.6480727232993, 13.5285727679729, 13.1794585380703,
    12.8222310449928, 12.3997843824327, 12.7413347829133, 14.3273916095495,
    17.3931313678622, 18.2263168506324, 18.5841742437333, 6.59096706658602,
    6.43405092414469, 6.25825286842883, 6.41100551001728, 6.47397979628295,
    10.5375754879788, 11.7441980168223, 12.6210678834468, 13.6038213036954,
    14.3639346119016, 14.6688716020435, 14.1826340463012, 15.2044224087149,
    15.5630568042397, 15.0458208750933, 10.0154311163351, 9.7418615128845,
    11.8866622913629, 10.4000290855765, 9.74880487192422, 12.071524746716,
    11.5644979756325, 11.0723461490124, 10.6282578315586, 10.2157085202634,
    14.5142644643784, 12.1188929770142, 12.3748247511685, 12.4087903182954,
    11.9534945581108, 9.04913682024926, 10.3765605948865, 11.6044067312032,
    11.8693192955106, 11.4852412138134, 9.60276927798986, 8.47671863157302,
    6.53922976925969, 6.61022553686053, 6.93009907845408, 13.2296028546989,
    13.0423339549452, 13.0597360432148, 12.6910961698741, 12.4157820828259,
    10.1926731644198, 8.71818219311535, 7.08254557102919, 8.77621911931783,
    10.0059285527095, 12.931788386777, 12.2630294412374, 11.4822425879538,
    10.4378029704094, 9.7940765786916, 13.0133786704391, 11.9061049539596,
    12.0638377033174, 12.3013137839735, 12.9490484017879, 13.2149957120419,
    13.1087802350521, 12.6286820042878, 12.2278920840472, 11.8682594038546,
    10.9492189250886, 12.2341319918633, 12.9464382771403, 12.5120461452752,
    12.5263502821326, 12.6686599105597, 12.7322974149138, 12.1948833111674,
    12.1215357910842, 11.9392029941082, 15.2677292469889, 16.3731585256755,
    17.8960581310093, 18.6334447469562, 19.5818214677274, 8.80653981585056,
    9.830889897421, 9.35642933472991, 8.49255602806807, 9.19627505354583,
    9.56638909410685, 10.4608207242563, 11.0053240321577, 12.0839668437839,
    12.6748947892338, 10.9087632503361, 11.0474556684494, 9.86553691327572,
    11.7183218244463, 12.5948534812778, 9.51134513597935, 7.67265690956265,
    8.47005187533796, 8.948102616705, 9.48919930960983, 8.92916852608323,
    9.19180226046592, 9.93818349670619, 10.3347131051123, 9.19244724791497,
    16.0914938896894, 16.6821955237538, 17.9938221350312, 19.0754321403801,
    19.048942392692, 8.59134346246719, 8.39548541698605, 8.17942153662443,
    8.02843223791569, 8.9953287737444, 7.97593365423381, 7.71139136049896,
    7.85907462704927, 8.38070099707693, 9.28482818417251, 11.3056178670377,
    11.601750086993, 11.2711317837238, 10.8186058234423, 10.7581429649144,
    15.6826636288315, 16.9076268095523, 15.4331855010241, 15.1698420289904,
    14.4226460717618, 11.3487603608519, 10.932231741026, 10.3945284616202,
    9.96728525497019, 9.48596934322268, 10.508708213456, 10.0394641282037,
    10.5090778553858, 10.1252990076318, 9.86525025218725, 21.985590364784,
    22.3454732447863, 22.693102620542, 22.8635905310512, 23.2176823541522,
    18.6908649746329, 16.1407203879207, 14.8633007425815, 13.0084274802357,
    10.3990704054013, 6.98735397309065, 6.87530469149351, 8.9313744334504,
    7.93048026971519, 8.05362006649375, 7.19595712143928, 6.09859018586576,
    7.31170470826328, 8.58990701381117, 8.4448722191155, 10.6643167790025,
    10.839969618246, 10.5106293456629, 10.4457534151152, 11.2185546196997,
    12.6707960385829, 12.9902018699795, 12.9533659201115, 12.501154281199,
    12.3501065187156, 25.9615670889616, 28.099115844816, 30.2258117124438,
    32.2391155175865, 34.1092220507562, 13.0570391658694, 14.2825467512012,
    11.1714780796319, 9.62660552468151, 13.1034480873495, 12.0462608523667,
    12.1476030908525, 12.087664520368, 12.486698012799, 12.6554797869176,
    12.9096878226846, 13.7426960282028, 15.2569429948926, 17.1046711038798,
    17.0782153028995, 8.75586932525039, 8.82860643323511, 8.69223182089627,
    9.15108947083354, 9.4462743261829, 8.55356580577791, 8.69411900639534,
    8.9102350641042, 9.00506707839668, 8.75238287262619, 12.8364848904312,
    14.6456281654537, 13.9498212374747, 14.5683591719717, 14.3893217202276,
    15.1805742178112, 16.7262759525329, 17.7521643228829, 18.5243777465075,
    18.8792126253247, 7.70680792629719, 7.47225251980126, 7.72799758706242,
    7.68415729980916, 7.50800217501819, 9.68811193015426, 10.5253741610795,
    10.922572016716, 10.9020531177521, 10.406608460471, 22.1927281469107,
    21.7946967110038, 22.5350291468203, 22.0015277154744, 23.2784972526133,
    25.1319196075201, 24.1645314730704, 23.0207713320851, 14.8746414575726,
    12.5255933962762, 19.3960575386882, 19.3368871696293, 19.8454126249999,
    19.8410699609667, 19.8172997217625, 12.1799279004335, 11.8857935070992,
    11.4909932948649, 11.3612791523337, 10.8840802218765, 11.1973982769996,
    11.6429010406137, 11.2867686431855, 11.5507948212326, 11.7122428491712,
    13.8513946440071, 14.9497504346073, 14.425096521154, 13.2822252810001,
    12.4311964027584, 18.864199379459, 17.5528808031231, 17.7616731729358,
    17.1655979007483, 16.6251927148551, 29.3679255992174, 28.4771841019392,
    27.9151875525713, 26.65377818048, 25.2528126351535, 10.6545137241483,
    10.91169398278, 11.0310669522732, 11.1646522767842, 11.2674177624285,
    13.7821182142943, 14.1553220339119, 15.0969068985432, 15.9642276819795,
    16.6291657369584, 9.4556876225397, 9.84383365139365, 11.0380863770843,
    10.6556000187993, 11.1149505246431, 8.38961955159903, 9.4479993218556,
    10.1951210992411, 10.6412279885262, 10.8386783860624, 8.28430177643895,
    8.50012865848839, 8.0173090333119, 8.15484160557389, 8.07647814508528,
    10.3200965328142, 10.4913098970428, 10.3476996067911, 10.6061836704612,
    12.1657092589885, 10.3872286621481, 9.38602960668504, 9.82730537652969,
    9.79454554617405, 9.12395850755274, 12.1763132046908, 12.7074157353491,
    12.6221365761012, 13.4234247263521, 15.5103187076747, 9.88674920517951,
    9.41792191006243, 8.58000149019063, 7.98727499786764, 7.34257609583437,
    13.8378750532866, 14.5356948953122, 14.5302697084844, 14.6059796679765,
    14.1489790286869, 14.9558734148741, 15.146628767252, 15.4630133416504,
    15.5585858970881, 15.4571908526123, 11.8359496816993, 11.2020426895469,
    11.4698356948793, 11.8119870778173, 13.0321650300175, 17.7426278125495,
    18.6734465416521, 18.8405636698008, 18.8715255819261, 18.9619445241988,
    8.8628712343052, 8.674994437024, 9.01558804325759, 9.04601749498397,
    8.85597188025713, 7.58305897470564, 7.92995095252991, 8.35649385116994,
    9.23873609863222, 9.14969765581191, 12.9726023878902, 12.2728526126593,
    13.0261426325887, 12.6654123421758, 11.5908016450703, 13.0077322013676,
    12.6599280629307, 11.9994106236845, 10.1917257998139, 9.89739338401705,
    10.7914459425956, 11.8336362764239, 11.7934257723391, 11.2242249771953,
    11.4056261256337, 7.95377462636679, 7.26088020019233, 7.43080170359462,
    7.50569254159927, 7.62218066956848, 11.2671461887658, 10.8180299866945,
    9.43983325269073, 9.29652785416692, 10.826626047492, 14.3595944624394,
    13.2217460777611, 12.7365244086832, 12.05212357454, 12.3027219437063,
    13.1963438820094, 12.8045422956347, 13.7076315935701, 14.145736489445,
    14.4983648322523, 14.3930621445179, 13.7241447810084, 13.0053710192442,
    12.2289746068418, 11.4307265728712, 22.3180065862834, 17.3237380106002,
    12.7182623371482, 13.0704908631742, 15.2839343994856, 11.1243085004389,
    10.2472041500732, 10.5197993572801, 11.790946405381, 10.6045705731958,
    15.1506495662034, 17.2426456119865, 18.0581725202501, 17.5418430939317,
    16.011631116271, 16.6771751828492, 14.9888406973332, 14.0024574939162,
    12.2754199896008, 10.462130815722, 14.700809167698, 14.7662508767098,
    14.6368321962655, 13.8920741155744, 13.6426123324782, 7.52487180288881,
    6.8714844295755, 7.11258086375892, 7.18187426682562, 7.26737848017365,
    8.01721725147218, 9.51534896157682, 9.49199174065143, 9.66430208645761,
    9.95999739971012, 12.6632636412978, 12.3405989259481, 12.1739520225674,
    11.8746338412166, 11.4930238109082, 17.375064175576, 16.5855303872377,
    14.6908791270107, 12.4465051107109, 10.6631374452263, 9.17110545560718,
    8.15483720507473, 8.49230268504471, 9.13922635372728, 9.57141006365418,
    16.033780714497, 17.3399481922388, 16.4341507013887, 15.3515323530883,
    14.7840439807624, 18.8009101431817, 19.3318882025778, 20.5749990418553,
    21.8101386912167, 21.9960610382259, 18.0659588892013, 17.8131891880184,
    17.4943805672228, 17.3403216060251, 16.8955769855529, 12.620489532128,
    12.2214950155467, 11.8860110174865, 11.3811555784196, 10.8314753975719,
    13.4036011062562, 11.5633060690016, 11.6371187847108, 12.5311543699354,
    13.4179203305393, 8.22134572081268, 7.50831649638712, 7.27005901280791,
    7.60287002194673, 7.99200239125639, 7.90263516828418, 8.68863912764937,
    10.4649641085416, 14.8291767574847, 13.2854715920985, 14.6683146245778,
    15.3950218576938, 16.1753460299224, 18.3709637727588, 18.7799926847219,
    9.85975402873009, 11.3263857085258, 14.0980262774974, 14.9891349021345,
    15.565140126273, 17.7682626061141, 17.6397152245045, 18.1632375810295,
    18.5020068660378, 18.6178280040622, 13.9469483401626, 13.3572864811867,
    13.7237298768014, 15.0745737366378, 13.0753238685429, 7.80682750046253,
    8.02811540197581, 8.54396957438439, 8.93615526147187, 9.23284823074937,
    11.9208830874413, 11.34336409159, 9.64633170515299, 9.77506830822676,
    9.60444209631532, 13.3866403251886, 13.6259520426393, 11.5198655985296,
    10.6700826901942, 9.85463059041649, 16.529045579955, 14.2629016656429,
    12.7639583777636, 13.6573225725442, 15.0617569684982, 9.50025964993984,
    9.68771148473024, 9.27095026709139, 9.30016769561917, 9.69172285404056,
    7.99956496339291, 7.4167326791212, 7.22712711431086, 8.56165643781424,
    9.04990502167493, 16.1096038296819, 15.6424694694579, 16.1224633455276,
    15.2468092739582, 15.2601830195636, 14.6924834232777, 15.2172856964171,
    15.6576700508595, 15.8558295574039, 15.6930990982801, 10.0672576809302,
    10.4989007581025, 10.7346505858004, 10.9321122989058, 10.1002658251673,
    7.57602006196976, 8.28179977834225, 9.00425424333662, 8.75011347234249,
    9.78429929818958, 8.22318575810641, 7.62580542359501, 7.52632019575685,
    7.3945076437667, 8.00606575794518, 9.82791453134269, 10.3108039358631,
    10.8194808941334, 11.0586643684655, 12.7866649534553, 16.4375944063067,
    16.122004436329, 15.8343450631946, 15.183718688786, 14.59901179187,
    13.086870778352, 13.8396339956671, 13.0286106839776, 12.6303931698203,
    11.8594408035278, 12.4039673712105, 9.90002802573144, 9.60356576833874,
    11.081666406244, 11.0487984493375, 15.9987502265722, 14.9749074596912,
    13.8462209142745, 12.3910789377987, 11.7417626548558, 10.7962236274034,
    11.77659323439, 11.0980827827007, 10.4603781597689, 10.4605271480978,
    12.797769298777, 11.2864379771054, 9.58062659483403, 9.57864196971059,
    9.7400170750916, 15.1035780552775, 15.3101249132305, 15.6179285142571,
    14.4825984723866, 11.6881796624511, 11.791490809992, 11.2104086671025,
    8.8539243908599, 8.34417999722064, 8.39954141993076, 9.41099112387747,
    8.93235134426504, 9.60718737915158, 9.41101815551519, 9.83936337288469,
    13.6638214811683, 14.4527215976268, 14.7365185897797, 13.2517122197896,
    11.0009524505585, 9.60110148880631, 8.54964307509363, 8.75000974629074,
    8.88564947526902, 7.84255138132721, 11.6202082950622, 12.075385870412,
    12.8382677212358, 14.9491381365806, 20.0978868640959, 8.93126882147044,
    9.09663643687963, 9.05409744009376, 8.98246862925589, 8.80278556142002,
    8.68155935313553, 8.91096869017929, 7.71334832534194, 9.87222944386303,
    11.2759735900909, 17.2249065712094, 17.9082475136966, 17.6210721954703,
    16.7172310408205, 16.2506423424929, 12.9267014097422, 14.7103695664555,
    19.504395313561, 22.4196153692901, 22.2453631460667, 8.23867111466825,
    8.10000761412084, 7.8771845670417, 7.56322089582682, 7.14911003597081,
    9.50618146453053, 8.6958515457809, 7.36113237217069, 6.79777669720352,
    6.69330381788313), .Dim = c(10L, 90L), .Dimnames = list(NULL,
    c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10",
    "X11", "X12", "X13", "X14", "X15", "X16", "X17", "X18", "X19",
    "X20", "X21", "X22", "X23", "X24", "X25", "X26", "X27", "X28",
    "X29", "X30", "X31", "X32", "X33", "X34", "X35", "X36", "X37",
    "X38", "X39", "X40", "X41", "X42", "X43", "X44", "X45", "X46",
    "X47", "X48", "X49", "X50", "X51", "X52", "X53", "X54", "X55",
    "X56", "X57", "X58", "X59", "X60", "X61", "X62", "X63", "X64",
    "X65", "X66", "X67", "X68", "X69", "X70", "X71", "X72", "X73",
    "X74", "X75", "X76", "X77", "X78", "X79", "X80", "X81", "X82",
    "X83", "X84", "X85", "X86", "X87", "X88", "X89", "X90")))

Is there any way to compute the means in this way? I just tried this, but I received the following error:
result <- rowMeans(cbind(c(subset), c(subset5)));dim(result) <- dim(subset);colnames(result) <- colnames(subset)

Error in rowMeans(cbind(c(subset), c(subset5))) : 'x' must be numeric

Thanks,
-----Original Message-----
From: Eric Berger <[hidden email]>
To: rain1290 <[hidden email]>
Cc: r-sig-geo <[hidden email]>; R mailing list <[hidden email]>
Sent: Fri, Apr 12, 2019 11:47 am
Subject: Re: [R] Creating a mean line plot

I don't have your data. Are the x-values the same in both plots?Does this example cover the situation?
f1 <- function(x) { x^3 - 2 }f2 <- function(x) { 2 - x^2 }
xV <- seq(from=0,to=2,length=50)y1 <- f1(xV)y2 <- f2(xV)y3 <- .5*(y1+y2)plot(x=xV,y=y1,col="blue",lwd=2,type='l',xlab="x",ylab="y")lines(x=xV,y=y2,col="green",lwd=2)lines(x=xV,y=y3,col="red",lwd=2)legend("topleft",legend=c("y1","y2","mean"),col=c("blue","green","red"),lwd=rep(2,3))
       

On Fri, Apr 12, 2019 at 5:34 PM rain1290--- via R-help <[hidden email]> wrote:

Hi there,
I am trying to create a mean line plot that shows the mean of a series of separate line plots that correspond to two climate models. Let's first try getting the mean of two line plots. To create the separate line plots, here is what I did to set up the x and y axis variables:

####Getting cumulative emissions data for x-axis: 1-dimensional ####

#For CanESM model#

ncfname <- "cumulative_emissions_1pctCO2.nc"
Model1 <- nc_open(ncfname)
get <- ncvar_get(Model1, "cum_co2_emi-CanESM2")     #units of terratones of carbon (TtC) for x-axis (140 values)
#For IPSL LR Model#
#Getting cumulative emissions data for x-axis IPSL LR 1pctCO2 IPSL <- ncvar_get(Model1, "cum_co2_emi-IPSL-CM5A-LR")     #units of terratones of carbon (TtC) for x-axis (140 values)

############################################################################################################

#####Getting precipitation data for y-axis - these are 3-dimensional####

#For CanESM2 model#
Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax")


#For IPSL LR Model#
Model10 <- brick("MaxPrecIPSLIPSL-CM5A-LR1pctCO2.nc", var="onedaymax")
#############################################################################################################
To create plots for a specific location:
lonlat <- cbind(103,3)          #specifies a specific longitude and latitude
Hope2 <- extract(Model2,lonlat)      #CanESM2
Hope6 <- extract(Model10,lonlat)   #start IPSL CM5A LR
plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative CO2 emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="One-day maximum precipitation for random location for 1pctCO2 scenario")
lines(IPSL, Hope6, type="l", lwd="3", col="green")
#############################################################################################################
So, the idea would be to create a plot that shows the mean of these two plots. Given what I showed above, how should I go about creating the mean of these two green line plots? Would you have to get the mean of the x-values, and then obtain the mean of the y-values, and then plot these?
Thanks, and any help would be greatly appreciated!
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a mean line plot

R help mailing list-2
So you're saying rowMeans(cbind(matrix_a, matrix_b)) worked to obtain
your X-axis values?

Wild guess here, are you simply looking for:
colMeans(rbind(matrix_a, matrix_b)) to obtain your Y-axis values?

[Above assuming matrix_a and matrix_b have identical dimensions (nrow, ncol)].

--Bill

William Michels, Ph.D.


On Fri, Apr 12, 2019 at 11:09 AM rain1290--- via R-help






<[hidden email]> wrote:

>
> Hi Eric,
>
> Ah, I apologize, and thank you for your response!
> I just figured out a way to average my x-values, so at least that is solved. I will still include the data for the two variables (1-dimensional) of interest that I was trying to average, just to show what was done:
> get2.teratons #(90 values)
> get5.teratons #(90 values)
> Here is what get2.teratons looks like (same idea for get5.teratons):
>     >print(get2.teratons)
>     [1] 0.4558545 0.4651129 0.4747509 0.4848242 0.4950900 0.5056109 0.5159335
>     0.5262532 0.5372275 0.5481839 0.5586787 0.5694379 0.5802970
>     [14] 0.5909211 0.6015753 0.6124256 0.6237733 0.6353634 0.6467227 0.6582857
>     0.6702509 0.6817027 0.6935311 0.7060161 0.7182312 0.7301909
>     [27] 0.7422574 0.7544744 0.7665907 0.7786409 0.7907518 0.8032732 0.8158733
>     0.8284363 0.8413905 0.8545881 0.8674711 0.8797701 0.8927392
>     [40] 0.9059937 0.9189707 0.9317215 0.9438155 0.9558035 0.9673665 0.9784927
>     0.9900898 1.0020388 1.0132683 1.0240023 1.0347708 1.0456077
>     [53] 1.0570347 1.0682903 1.0793535 1.0901511 1.1001753 1.1101276 1.1199142
>     1.1293237 1.1384669 1.1470002 1.1547341 1.1622488 1.1697549
>     [66] 1.1777542 1.1857587 1.1930233 1.1999645 1.2067172 1.2132979 1.2199317
>     1.2265673 1.2328599 1.2390689 1.2446050 1.2495579 1.2546455
>     [79] 1.2599212 1.2648733 1.2700068 1.2753889 1.2807509 1.2856922 1.2905927
>     1.2953338 1.3000484 1.3045992 1.3091128 1.3144190
> The following worked in terms of averaging all of the elements of get2.teratons and get5.teratons:
> rowMeans(cbind(get2.teratons,get5.teratons))
> However, I am trying to do something similar for the values on my y-axis. So, for now, here are the two variables (3-dimensional) that I would like to average:
>     subset
>     subset5
> Using the print function for "subset" (same idea for subset5):    >print(subset)
>     class       : RasterStack
>     dimensions  : 64, 128, 8192, 90  (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
>     names       : X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14,
>     X15, ...     >dim(subset)
>     [1]  64 128  90>dim(subset5)
>     [1]  64 128  90
> I tried `mean(subset,subset5)`, which works, BUT it combines the 90 layers into 1 layer. I want keep the number of layers at 90, but simply average each of the grid cell values of "subset" and "subset5" for each layer. So, for instance, I want to average the values of each grid cell of layer 1 of "subset" with the values of each grid cell of layer 1 of "subset5", and then average those values of layer 2 of "subset" with those values of layer 2 of "subset5"......all the way to layer 90. That way, I have 90 averages across all grid cells.
> Here is what the data looks like for "subset":
> >dput(head(subset,5))
>     structure(c(11.5447145886719, 11.2479725852609, 10.0223480723798,
>     11.4909216295928, 12.5930442474782, 15.0295264553279, 14.6107862703502,
>     13.3623332250863, 10.4473929153755, 13.262210553512, 13.3166334126145,
>     13.7211008928716, 10.594900790602, 11.7217378690839, 10.8397546224296,
>     14.2727348953485, 13.6185416020453, 12.7485566306859, 11.7246472276747,
>     10.6815265025944, 13.1605062168092, 12.9131189547479, 12.6493454910815,
>     11.6938022430986, 11.4522186107934, 8.84930260945112, 11.5785481408238,
>     12.9859233275056, 13.6702361516654, 11.863912967965, 11.6624090820551,
>     12.1465771459043, 12.9789240192622, 13.5916746687144, 15.0383287109435,
>     7.89674604311585, 8.14079332631081, 7.05628590658307, 6.99759456329048,
>     8.06435288395733, 8.00622920505702, 7.35754533670843, 6.57949370797724,
>     6.26998774241656, 6.10911303665489, 10.1576759945601, 9.83650996349752,
>     10.6277788057923, 10.3647025069222, 9.38627037685364, 28.411143925041,
>     27.3436004295945, 25.7670222781599, 24.1854049265385, 22.7183715440333,
>     10.8529561199248, 11.1584928352386, 11.4545458462089, 11.7570801638067,
>     11.6314635146409, 13.7268429156393, 12.4547378160059, 12.8433785866946,
>     10.282119596377, 9.66278391424567, 6.39572446234524, 8.4569685626775,
>     12.253624945879, 12.4784250743687, 13.6823802720755, 8.65540341474116,
>     8.34308553021401, 8.30261853989214, 7.9798299819231, 7.96007991302758,
>     13.3976918645203, 15.2056947816163, 15.3097502421588, 18.0296610575169,
>     17.918016621843, 14.121591579169, 14.3091559410095, 14.7470911033452,
>     15.414851764217, 15.8059203531593, 22.9126498103142, 21.5608592145145,
>     19.7303873486817, 17.5689237657934, 15.4688697773963, 10.2526041911915,
>     10.4463449679315, 9.85705149360001, 9.5394266070798, 9.17961853556335,
>     14.064371259883, 12.626935634762, 12.1540617663413, 10.9235350973904,
>     9.32216013316065, 12.3676003888249, 12.9718807060272, 14.5685050170869,
>     13.8497828040272, 14.0683455392718, 8.09576804749668, 8.54510050266981,
>     8.02388715092093, 8.6679536383599, 9.38348234631121, 11.6279292851686,
>     11.5998465567827, 11.6469369269907, 11.6286710835993, 10.8152111526579,
>     17.4072104506195, 18.9169261604548, 19.5168524980545, 19.0377978142351,
>     19.5594304706901, 9.74474258255213, 10.2144323755056, 10.9722976572812,
>     11.5369332488626, 12.0274581480771, 14.007618650794, 14.0536692459136,
>     14.4861201290041, 14.133819937706, 13.045089924708, 19.9330265633762,
>     20.3158976510167, 21.4452845044434, 19.9475897010416, 20.3566399868578,
>     15.703826257959, 14.8260951507837, 14.6203982178122, 14.0476305037737,
>     13.2086589932442, 6.5044054761529, 6.51829722337425, 6.59741191193461,
>     6.57343484926969, 7.07112564705312, 8.42645864468068, 9.15604883339256,
>     10.8542435802519, 8.57339131180197, 7.89698304142803, 10.6029914226383,
>     9.90388663485646, 8.46301421988755, 12.9162973724306, 9.06370310112834,
>     9.92726711556315, 11.5754703059793, 8.74886247329414, 8.99941809475422,
>     9.90840594749898, 11.1468604300171, 11.1322306562215, 10.49438144546,
>     9.50155213940889, 8.31737467087805, 5.76932597905397, 6.14411209244281,
>     7.39980584476143, 8.47632132936269, 8.00714262295514, 8.64454926922917,
>     7.79559868387878, 7.14818593114614, 7.42282171268016, 9.04718739911914,
>     12.0141573250294, 11.0411503817886, 11.7892528418452, 11.2668004352599,
>     10.5345542309806, 14.2355003859848, 12.4114783946425, 13.1144292186946,
>     14.3049817532301, 14.7282858844846, 9.90791183430701, 10.4058899218217,
>     12.0624131988734, 13.2521220948547, 13.9345653355122, 12.5256763771176,
>     12.3285478446633, 11.9927407242358, 11.6441268939525, 11.6448875516653,
>     30.5602320469916, 30.6964941322803, 27.3358505219221, 27.5474566966295,
>     24.3847575969994, 15.1250814087689, 15.0272130500525, 14.9795342702419,
>     14.2658210825175, 13.437497522682, 10.7001833617687, 10.0823557935655,
>     10.1298170629889, 9.99525294173509, 10.6919908896089, 9.04134479351342,
>     9.57930330187082, 9.58402880933136, 8.82056106347591, 9.06912200152874,
>     11.0435656271875, 12.827942892909, 14.6962288767099, 15.984565531835,
>     16.3673574104905, 17.7882182411849, 17.1887206379324, 16.4347139652818,
>     15.4833788517863, 14.3649869598448, 10.0324214436114, 10.9937381464988,
>     10.7803415972739, 10.64134365879, 10.3700830601156, 10.7242427766323,
>     10.1225153775886, 9.59254063200206, 9.67734202276915, 9.9705743137747,
>     6.15209711249918, 7.6417050557211, 9.55170588567853, 12.123644258827,
>     14.6793850231916, 13.8236853294075, 14.3564789090306, 13.6828002054244,
>     13.0476749036461, 12.3909330926836, 12.5938401091844, 12.5098232645541,
>     12.4792913440615, 10.5595408938825, 10.0890464382246, 9.20089432038367,
>     8.92592284362763, 8.59467086847872, 9.42603517323732, 10.0353622343391,
>     11.7311725392938, 12.4379832297564, 12.9343897104263, 12.9055073484778,
>     10.8944955747575, 13.6480727232993, 13.5285727679729, 13.1794585380703,
>     12.8222310449928, 12.3997843824327, 12.7413347829133, 14.3273916095495,
>     17.3931313678622, 18.2263168506324, 18.5841742437333, 6.59096706658602,
>     6.43405092414469, 6.25825286842883, 6.41100551001728, 6.47397979628295,
>     10.5375754879788, 11.7441980168223, 12.6210678834468, 13.6038213036954,
>     14.3639346119016, 14.6688716020435, 14.1826340463012, 15.2044224087149,
>     15.5630568042397, 15.0458208750933, 10.0154311163351, 9.7418615128845,
>     11.8866622913629, 10.4000290855765, 9.74880487192422, 12.071524746716,
>     11.5644979756325, 11.0723461490124, 10.6282578315586, 10.2157085202634,
>     14.5142644643784, 12.1188929770142, 12.3748247511685, 12.4087903182954,
>     11.9534945581108, 9.04913682024926, 10.3765605948865, 11.6044067312032,
>     11.8693192955106, 11.4852412138134, 9.60276927798986, 8.47671863157302,
>     6.53922976925969, 6.61022553686053, 6.93009907845408, 13.2296028546989,
>     13.0423339549452, 13.0597360432148, 12.6910961698741, 12.4157820828259,
>     10.1926731644198, 8.71818219311535, 7.08254557102919, 8.77621911931783,
>     10.0059285527095, 12.931788386777, 12.2630294412374, 11.4822425879538,
>     10.4378029704094, 9.7940765786916, 13.0133786704391, 11.9061049539596,
>     12.0638377033174, 12.3013137839735, 12.9490484017879, 13.2149957120419,
>     13.1087802350521, 12.6286820042878, 12.2278920840472, 11.8682594038546,
>     10.9492189250886, 12.2341319918633, 12.9464382771403, 12.5120461452752,
>     12.5263502821326, 12.6686599105597, 12.7322974149138, 12.1948833111674,
>     12.1215357910842, 11.9392029941082, 15.2677292469889, 16.3731585256755,
>     17.8960581310093, 18.6334447469562, 19.5818214677274, 8.80653981585056,
>     9.830889897421, 9.35642933472991, 8.49255602806807, 9.19627505354583,
>     9.56638909410685, 10.4608207242563, 11.0053240321577, 12.0839668437839,
>     12.6748947892338, 10.9087632503361, 11.0474556684494, 9.86553691327572,
>     11.7183218244463, 12.5948534812778, 9.51134513597935, 7.67265690956265,
>     8.47005187533796, 8.948102616705, 9.48919930960983, 8.92916852608323,
>     9.19180226046592, 9.93818349670619, 10.3347131051123, 9.19244724791497,
>     16.0914938896894, 16.6821955237538, 17.9938221350312, 19.0754321403801,
>     19.048942392692, 8.59134346246719, 8.39548541698605, 8.17942153662443,
>     8.02843223791569, 8.9953287737444, 7.97593365423381, 7.71139136049896,
>     7.85907462704927, 8.38070099707693, 9.28482818417251, 11.3056178670377,
>     11.601750086993, 11.2711317837238, 10.8186058234423, 10.7581429649144,
>     15.6826636288315, 16.9076268095523, 15.4331855010241, 15.1698420289904,
>     14.4226460717618, 11.3487603608519, 10.932231741026, 10.3945284616202,
>     9.96728525497019, 9.48596934322268, 10.508708213456, 10.0394641282037,
>     10.5090778553858, 10.1252990076318, 9.86525025218725, 21.985590364784,
>     22.3454732447863, 22.693102620542, 22.8635905310512, 23.2176823541522,
>     18.6908649746329, 16.1407203879207, 14.8633007425815, 13.0084274802357,
>     10.3990704054013, 6.98735397309065, 6.87530469149351, 8.9313744334504,
>     7.93048026971519, 8.05362006649375, 7.19595712143928, 6.09859018586576,
>     7.31170470826328, 8.58990701381117, 8.4448722191155, 10.6643167790025,
>     10.839969618246, 10.5106293456629, 10.4457534151152, 11.2185546196997,
>     12.6707960385829, 12.9902018699795, 12.9533659201115, 12.501154281199,
>     12.3501065187156, 25.9615670889616, 28.099115844816, 30.2258117124438,
>     32.2391155175865, 34.1092220507562, 13.0570391658694, 14.2825467512012,
>     11.1714780796319, 9.62660552468151, 13.1034480873495, 12.0462608523667,
>     12.1476030908525, 12.087664520368, 12.486698012799, 12.6554797869176,
>     12.9096878226846, 13.7426960282028, 15.2569429948926, 17.1046711038798,
>     17.0782153028995, 8.75586932525039, 8.82860643323511, 8.69223182089627,
>     9.15108947083354, 9.4462743261829, 8.55356580577791, 8.69411900639534,
>     8.9102350641042, 9.00506707839668, 8.75238287262619, 12.8364848904312,
>     14.6456281654537, 13.9498212374747, 14.5683591719717, 14.3893217202276,
>     15.1805742178112, 16.7262759525329, 17.7521643228829, 18.5243777465075,
>     18.8792126253247, 7.70680792629719, 7.47225251980126, 7.72799758706242,
>     7.68415729980916, 7.50800217501819, 9.68811193015426, 10.5253741610795,
>     10.922572016716, 10.9020531177521, 10.406608460471, 22.1927281469107,
>     21.7946967110038, 22.5350291468203, 22.0015277154744, 23.2784972526133,
>     25.1319196075201, 24.1645314730704, 23.0207713320851, 14.8746414575726,
>     12.5255933962762, 19.3960575386882, 19.3368871696293, 19.8454126249999,
>     19.8410699609667, 19.8172997217625, 12.1799279004335, 11.8857935070992,
>     11.4909932948649, 11.3612791523337, 10.8840802218765, 11.1973982769996,
>     11.6429010406137, 11.2867686431855, 11.5507948212326, 11.7122428491712,
>     13.8513946440071, 14.9497504346073, 14.425096521154, 13.2822252810001,
>     12.4311964027584, 18.864199379459, 17.5528808031231, 17.7616731729358,
>     17.1655979007483, 16.6251927148551, 29.3679255992174, 28.4771841019392,
>     27.9151875525713, 26.65377818048, 25.2528126351535, 10.6545137241483,
>     10.91169398278, 11.0310669522732, 11.1646522767842, 11.2674177624285,
>     13.7821182142943, 14.1553220339119, 15.0969068985432, 15.9642276819795,
>     16.6291657369584, 9.4556876225397, 9.84383365139365, 11.0380863770843,
>     10.6556000187993, 11.1149505246431, 8.38961955159903, 9.4479993218556,
>     10.1951210992411, 10.6412279885262, 10.8386783860624, 8.28430177643895,
>     8.50012865848839, 8.0173090333119, 8.15484160557389, 8.07647814508528,
>     10.3200965328142, 10.4913098970428, 10.3476996067911, 10.6061836704612,
>     12.1657092589885, 10.3872286621481, 9.38602960668504, 9.82730537652969,
>     9.79454554617405, 9.12395850755274, 12.1763132046908, 12.7074157353491,
>     12.6221365761012, 13.4234247263521, 15.5103187076747, 9.88674920517951,
>     9.41792191006243, 8.58000149019063, 7.98727499786764, 7.34257609583437,
>     13.8378750532866, 14.5356948953122, 14.5302697084844, 14.6059796679765,
>     14.1489790286869, 14.9558734148741, 15.146628767252, 15.4630133416504,
>     15.5585858970881, 15.4571908526123, 11.8359496816993, 11.2020426895469,
>     11.4698356948793, 11.8119870778173, 13.0321650300175, 17.7426278125495,
>     18.6734465416521, 18.8405636698008, 18.8715255819261, 18.9619445241988,
>     8.8628712343052, 8.674994437024, 9.01558804325759, 9.04601749498397,
>     8.85597188025713, 7.58305897470564, 7.92995095252991, 8.35649385116994,
>     9.23873609863222, 9.14969765581191, 12.9726023878902, 12.2728526126593,
>     13.0261426325887, 12.6654123421758, 11.5908016450703, 13.0077322013676,
>     12.6599280629307, 11.9994106236845, 10.1917257998139, 9.89739338401705,
>     10.7914459425956, 11.8336362764239, 11.7934257723391, 11.2242249771953,
>     11.4056261256337, 7.95377462636679, 7.26088020019233, 7.43080170359462,
>     7.50569254159927, 7.62218066956848, 11.2671461887658, 10.8180299866945,
>     9.43983325269073, 9.29652785416692, 10.826626047492, 14.3595944624394,
>     13.2217460777611, 12.7365244086832, 12.05212357454, 12.3027219437063,
>     13.1963438820094, 12.8045422956347, 13.7076315935701, 14.145736489445,
>     14.4983648322523, 14.3930621445179, 13.7241447810084, 13.0053710192442,
>     12.2289746068418, 11.4307265728712, 22.3180065862834, 17.3237380106002,
>     12.7182623371482, 13.0704908631742, 15.2839343994856, 11.1243085004389,
>     10.2472041500732, 10.5197993572801, 11.790946405381, 10.6045705731958,
>     15.1506495662034, 17.2426456119865, 18.0581725202501, 17.5418430939317,
>     16.011631116271, 16.6771751828492, 14.9888406973332, 14.0024574939162,
>     12.2754199896008, 10.462130815722, 14.700809167698, 14.7662508767098,
>     14.6368321962655, 13.8920741155744, 13.6426123324782, 7.52487180288881,
>     6.8714844295755, 7.11258086375892, 7.18187426682562, 7.26737848017365,
>     8.01721725147218, 9.51534896157682, 9.49199174065143, 9.66430208645761,
>     9.95999739971012, 12.6632636412978, 12.3405989259481, 12.1739520225674,
>     11.8746338412166, 11.4930238109082, 17.375064175576, 16.5855303872377,
>     14.6908791270107, 12.4465051107109, 10.6631374452263, 9.17110545560718,
>     8.15483720507473, 8.49230268504471, 9.13922635372728, 9.57141006365418,
>     16.033780714497, 17.3399481922388, 16.4341507013887, 15.3515323530883,
>     14.7840439807624, 18.8009101431817, 19.3318882025778, 20.5749990418553,
>     21.8101386912167, 21.9960610382259, 18.0659588892013, 17.8131891880184,
>     17.4943805672228, 17.3403216060251, 16.8955769855529, 12.620489532128,
>     12.2214950155467, 11.8860110174865, 11.3811555784196, 10.8314753975719,
>     13.4036011062562, 11.5633060690016, 11.6371187847108, 12.5311543699354,
>     13.4179203305393, 8.22134572081268, 7.50831649638712, 7.27005901280791,
>     7.60287002194673, 7.99200239125639, 7.90263516828418, 8.68863912764937,
>     10.4649641085416, 14.8291767574847, 13.2854715920985, 14.6683146245778,
>     15.3950218576938, 16.1753460299224, 18.3709637727588, 18.7799926847219,
>     9.85975402873009, 11.3263857085258, 14.0980262774974, 14.9891349021345,
>     15.565140126273, 17.7682626061141, 17.6397152245045, 18.1632375810295,
>     18.5020068660378, 18.6178280040622, 13.9469483401626, 13.3572864811867,
>     13.7237298768014, 15.0745737366378, 13.0753238685429, 7.80682750046253,
>     8.02811540197581, 8.54396957438439, 8.93615526147187, 9.23284823074937,
>     11.9208830874413, 11.34336409159, 9.64633170515299, 9.77506830822676,
>     9.60444209631532, 13.3866403251886, 13.6259520426393, 11.5198655985296,
>     10.6700826901942, 9.85463059041649, 16.529045579955, 14.2629016656429,
>     12.7639583777636, 13.6573225725442, 15.0617569684982, 9.50025964993984,
>     9.68771148473024, 9.27095026709139, 9.30016769561917, 9.69172285404056,
>     7.99956496339291, 7.4167326791212, 7.22712711431086, 8.56165643781424,
>     9.04990502167493, 16.1096038296819, 15.6424694694579, 16.1224633455276,
>     15.2468092739582, 15.2601830195636, 14.6924834232777, 15.2172856964171,
>     15.6576700508595, 15.8558295574039, 15.6930990982801, 10.0672576809302,
>     10.4989007581025, 10.7346505858004, 10.9321122989058, 10.1002658251673,
>     7.57602006196976, 8.28179977834225, 9.00425424333662, 8.75011347234249,
>     9.78429929818958, 8.22318575810641, 7.62580542359501, 7.52632019575685,
>     7.3945076437667, 8.00606575794518, 9.82791453134269, 10.3108039358631,
>     10.8194808941334, 11.0586643684655, 12.7866649534553, 16.4375944063067,
>     16.122004436329, 15.8343450631946, 15.183718688786, 14.59901179187,
>     13.086870778352, 13.8396339956671, 13.0286106839776, 12.6303931698203,
>     11.8594408035278, 12.4039673712105, 9.90002802573144, 9.60356576833874,
>     11.081666406244, 11.0487984493375, 15.9987502265722, 14.9749074596912,
>     13.8462209142745, 12.3910789377987, 11.7417626548558, 10.7962236274034,
>     11.77659323439, 11.0980827827007, 10.4603781597689, 10.4605271480978,
>     12.797769298777, 11.2864379771054, 9.58062659483403, 9.57864196971059,
>     9.7400170750916, 15.1035780552775, 15.3101249132305, 15.6179285142571,
>     14.4825984723866, 11.6881796624511, 11.791490809992, 11.2104086671025,
>     8.8539243908599, 8.34417999722064, 8.39954141993076, 9.41099112387747,
>     8.93235134426504, 9.60718737915158, 9.41101815551519, 9.83936337288469,
>     13.6638214811683, 14.4527215976268, 14.7365185897797, 13.2517122197896,
>     11.0009524505585, 9.60110148880631, 8.54964307509363, 8.75000974629074,
>     8.88564947526902, 7.84255138132721, 11.6202082950622, 12.075385870412,
>     12.8382677212358, 14.9491381365806, 20.0978868640959, 8.93126882147044,
>     9.09663643687963, 9.05409744009376, 8.98246862925589, 8.80278556142002,
>     8.68155935313553, 8.91096869017929, 7.71334832534194, 9.87222944386303,
>     11.2759735900909, 17.2249065712094, 17.9082475136966, 17.6210721954703,
>     16.7172310408205, 16.2506423424929, 12.9267014097422, 14.7103695664555,
>     19.504395313561, 22.4196153692901, 22.2453631460667, 8.23867111466825,
>     8.10000761412084, 7.8771845670417, 7.56322089582682, 7.14911003597081,
>     9.50618146453053, 8.6958515457809, 7.36113237217069, 6.79777669720352,
>     6.69330381788313), .Dim = c(10L, 90L), .Dimnames = list(NULL,
>     c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10",
>     "X11", "X12", "X13", "X14", "X15", "X16", "X17", "X18", "X19",
>     "X20", "X21", "X22", "X23", "X24", "X25", "X26", "X27", "X28",
>     "X29", "X30", "X31", "X32", "X33", "X34", "X35", "X36", "X37",
>     "X38", "X39", "X40", "X41", "X42", "X43", "X44", "X45", "X46",
>     "X47", "X48", "X49", "X50", "X51", "X52", "X53", "X54", "X55",
>     "X56", "X57", "X58", "X59", "X60", "X61", "X62", "X63", "X64",
>     "X65", "X66", "X67", "X68", "X69", "X70", "X71", "X72", "X73",
>     "X74", "X75", "X76", "X77", "X78", "X79", "X80", "X81", "X82",
>     "X83", "X84", "X85", "X86", "X87", "X88", "X89", "X90")))
>
> Is there any way to compute the means in this way? I just tried this, but I received the following error:
> result <- rowMeans(cbind(c(subset), c(subset5)));dim(result) <- dim(subset);colnames(result) <- colnames(subset)
>
> Error in rowMeans(cbind(c(subset), c(subset5))) : 'x' must be numeric
>
> Thanks,
> -----Original Message-----
> From: Eric Berger <[hidden email]>
> To: rain1290 <[hidden email]>
> Cc: r-sig-geo <[hidden email]>; R mailing list <[hidden email]>
> Sent: Fri, Apr 12, 2019 11:47 am
> Subject: Re: [R] Creating a mean line plot
>
> I don't have your data. Are the x-values the same in both plots?Does this example cover the situation?
> f1 <- function(x) { x^3 - 2 }f2 <- function(x) { 2 - x^2 }
> xV <- seq(from=0,to=2,length=50)y1 <- f1(xV)y2 <- f2(xV)y3 <- .5*(y1+y2)plot(x=xV,y=y1,col="blue",lwd=2,type='l',xlab="x",ylab="y")lines(x=xV,y=y2,col="green",lwd=2)lines(x=xV,y=y3,col="red",lwd=2)legend("topleft",legend=c("y1","y2","mean"),col=c("blue","green","red"),lwd=rep(2,3))
>
>
> On Fri, Apr 12, 2019 at 5:34 PM rain1290--- via R-help <[hidden email]> wrote:
>
> Hi there,
> I am trying to create a mean line plot that shows the mean of a series of separate line plots that correspond to two climate models. Let's first try getting the mean of two line plots. To create the separate line plots, here is what I did to set up the x and y axis variables:
>
> ####Getting cumulative emissions data for x-axis: 1-dimensional ####
>
> #For CanESM model#
>
> ncfname <- "cumulative_emissions_1pctCO2.nc"
> Model1 <- nc_open(ncfname)
> get <- ncvar_get(Model1, "cum_co2_emi-CanESM2")     #units of terratones of carbon (TtC) for x-axis (140 values)
> #For IPSL LR Model#
> #Getting cumulative emissions data for x-axis IPSL LR 1pctCO2 IPSL <- ncvar_get(Model1, "cum_co2_emi-IPSL-CM5A-LR")     #units of terratones of carbon (TtC) for x-axis (140 values)
>
> ############################################################################################################
>
> #####Getting precipitation data for y-axis - these are 3-dimensional####
>
> #For CanESM2 model#
> Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax")
>
>
> #For IPSL LR Model#
> Model10 <- brick("MaxPrecIPSLIPSL-CM5A-LR1pctCO2.nc", var="onedaymax")
> #############################################################################################################
> To create plots for a specific location:
> lonlat <- cbind(103,3)          #specifies a specific longitude and latitude
> Hope2 <- extract(Model2,lonlat)      #CanESM2
> Hope6 <- extract(Model10,lonlat)   #start IPSL CM5A LR
> plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative CO2 emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="One-day maximum precipitation for random location for 1pctCO2 scenario")
> lines(IPSL, Hope6, type="l", lwd="3", col="green")
> #############################################################################################################
> So, the idea would be to create a plot that shows the mean of these two plots. Given what I showed above, how should I go about creating the mean of these two green line plots? Would you have to get the mean of the x-values, and then obtain the mean of the y-values, and then plot these?
> Thanks, and any help would be greatly appreciated!
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a mean line plot

R help mailing list-2
In reply to this post by R help mailing list-2
Hi Bill,

For the x-axis variable, in this case, indeed, I used rowMeans(cbind(get2.teratons, get5.teratons)). This averaged each value between these two 1-dimensional variables (i.e. value#1 of "get2.teratons" was averaged with value#1 of "get5.teratons" - this was done for all 90 values). 
To obtain the means for the values of the y-axis variables, which are 3-dimensional, I simply took each variable and divided by 2, in this case. Thus: (variableA+variableB)/2. This took the mean of the variable for each grid cell for each layer (90 layers). So, for grid cell #1, doing this averaged all 90 values corresponding to the 90 layers between the two variables. For example, the values of layer 1 of variableA and layer 1 of variableB were averaged (and then layer 2 with layer 2, and then layer 3 with layer 3.....all the way to layer 90 with layer 90. This method simultaneously did this for all 8192 grid cells (128 lines of longitude and 64 lines of latitude). At the end, I obtained 90 averages for each grid cell. :)
~Trav.~
-----Original Message-----
From: William Michels <[hidden email]>
To: rain1290 <[hidden email]>
Cc: r-help <[hidden email]>; r-sig-geo <[hidden email]>
Sent: Sun, Apr 14, 2019 4:46 am
Subject: Re: [R] Creating a mean line plot

So you're saying rowMeans(cbind(matrix_a, matrix_b)) worked to obtain
your X-axis values?

Wild guess here, are you simply looking for:
colMeans(rbind(matrix_a, matrix_b)) to obtain your Y-axis values?

[Above assuming matrix_a and matrix_b have identical dimensions (nrow, ncol)].

--Bill

William Michels, Ph.D.


On Fri, Apr 12, 2019 at 11:09 AM rain1290--- via R-help






<[hidden email]> wrote:

>
> Hi Eric,
>
> Ah, I apologize, and thank you for your response!
> I just figured out a way to average my x-values, so at least that is solved. I will still include the data for the two variables (1-dimensional) of interest that I was trying to average, just to show what was done:
> get2.teratons #(90 values)
> get5.teratons #(90 values)
> Here is what get2.teratons looks like (same idea for get5.teratons):
>    >print(get2.teratons)
>    [1] 0.4558545 0.4651129 0.4747509 0.4848242 0.4950900 0.5056109 0.5159335
>    0.5262532 0.5372275 0.5481839 0.5586787 0.5694379 0.5802970
>    [14] 0.5909211 0.6015753 0.6124256 0.6237733 0.6353634 0.6467227 0.6582857
>    0.6702509 0.6817027 0.6935311 0.7060161 0.7182312 0.7301909
>    [27] 0.7422574 0.7544744 0.7665907 0.7786409 0.7907518 0.8032732 0.8158733
>    0.8284363 0.8413905 0.8545881 0.8674711 0.8797701 0.8927392
>    [40] 0.9059937 0.9189707 0.9317215 0.9438155 0.9558035 0.9673665 0.9784927
>    0.9900898 1.0020388 1.0132683 1.0240023 1.0347708 1.0456077
>    [53] 1.0570347 1.0682903 1.0793535 1.0901511 1.1001753 1.1101276 1.1199142
>    1.1293237 1.1384669 1.1470002 1.1547341 1.1622488 1.1697549
>    [66] 1.1777542 1.1857587 1.1930233 1.1999645 1.2067172 1.2132979 1.2199317
>    1.2265673 1.2328599 1.2390689 1.2446050 1.2495579 1.2546455
>    [79] 1.2599212 1.2648733 1.2700068 1.2753889 1.2807509 1.2856922 1.2905927
>    1.2953338 1.3000484 1.3045992 1.3091128 1.3144190
> The following worked in terms of averaging all of the elements of get2.teratons and get5.teratons:
> rowMeans(cbind(get2.teratons,get5.teratons))
> However, I am trying to do something similar for the values on my y-axis. So, for now, here are the two variables (3-dimensional) that I would like to average:
>    subset
>    subset5
> Using the print function for "subset" (same idea for subset5):    >print(subset)
>    class      : RasterStack
>    dimensions  : 64, 128, 8192, 90  (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
>    names      : X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14,
>    X15, ...    >dim(subset)
>    [1]  64 128  90>dim(subset5)
>    [1]  64 128  90
> I tried `mean(subset,subset5)`, which works, BUT it combines the 90 layers into 1 layer. I want keep the number of layers at 90, but simply average each of the grid cell values of "subset" and "subset5" for each layer. So, for instance, I want to average the values of each grid cell of layer 1 of "subset" with the values of each grid cell of layer 1 of "subset5", and then average those values of layer 2 of "subset" with those values of layer 2 of "subset5"......all the way to layer 90. That way, I have 90 averages across all grid cells.
> Here is what the data looks like for "subset":
> >dput(head(subset,5))
>    structure(c(11.5447145886719, 11.2479725852609, 10.0223480723798,
>    11.4909216295928, 12.5930442474782, 15.0295264553279, 14.6107862703502,
>    13.3623332250863, 10.4473929153755, 13.262210553512, 13.3166334126145,
>    13.7211008928716, 10.594900790602, 11.7217378690839, 10.8397546224296,
>    14.2727348953485, 13.6185416020453, 12.7485566306859, 11.7246472276747,
>    10.6815265025944, 13.1605062168092, 12.9131189547479, 12.6493454910815,
>    11.6938022430986, 11.4522186107934, 8.84930260945112, 11.5785481408238,
>    12.9859233275056, 13.6702361516654, 11.863912967965, 11.6624090820551,
>    12.1465771459043, 12.9789240192622, 13.5916746687144, 15.0383287109435,
>    7.89674604311585, 8.14079332631081, 7.05628590658307, 6.99759456329048,
>    8.06435288395733, 8.00622920505702, 7.35754533670843, 6.57949370797724,
>    6.26998774241656, 6.10911303665489, 10.1576759945601, 9.83650996349752,
>    10.6277788057923, 10.3647025069222, 9.38627037685364, 28.411143925041,
>    27.3436004295945, 25.7670222781599, 24.1854049265385, 22.7183715440333,
>    10.8529561199248, 11.1584928352386, 11.4545458462089, 11.7570801638067,
>    11.6314635146409, 13.7268429156393, 12.4547378160059, 12.8433785866946,
>    10.282119596377, 9.66278391424567, 6.39572446234524, 8.4569685626775,
>    12.253624945879, 12.4784250743687, 13.6823802720755, 8.65540341474116,
>    8.34308553021401, 8.30261853989214, 7.9798299819231, 7.96007991302758,
>    13.3976918645203, 15.2056947816163, 15.3097502421588, 18.0296610575169,
>    17.918016621843, 14.121591579169, 14.3091559410095, 14.7470911033452,
>    15.414851764217, 15.8059203531593, 22.9126498103142, 21.5608592145145,
>    19.7303873486817, 17.5689237657934, 15.4688697773963, 10.2526041911915,
>    10.4463449679315, 9.85705149360001, 9.5394266070798, 9.17961853556335,
>    14.064371259883, 12.626935634762, 12.1540617663413, 10.9235350973904,
>    9.32216013316065, 12.3676003888249, 12.9718807060272, 14.5685050170869,
>    13.8497828040272, 14.0683455392718, 8.09576804749668, 8.54510050266981,
>    8.02388715092093, 8.6679536383599, 9.38348234631121, 11.6279292851686,
>    11.5998465567827, 11.6469369269907, 11.6286710835993, 10.8152111526579,
>    17.4072104506195, 18.9169261604548, 19.5168524980545, 19.0377978142351,
>    19.5594304706901, 9.74474258255213, 10.2144323755056, 10.9722976572812,
>    11.5369332488626, 12.0274581480771, 14.007618650794, 14.0536692459136,
>    14.4861201290041, 14.133819937706, 13.045089924708, 19.9330265633762,
>    20.3158976510167, 21.4452845044434, 19.9475897010416, 20.3566399868578,
>    15.703826257959, 14.8260951507837, 14.6203982178122, 14.0476305037737,
>    13.2086589932442, 6.5044054761529, 6.51829722337425, 6.59741191193461,
>    6.57343484926969, 7.07112564705312, 8.42645864468068, 9.15604883339256,
>    10.8542435802519, 8.57339131180197, 7.89698304142803, 10.6029914226383,
>    9.90388663485646, 8.46301421988755, 12.9162973724306, 9.06370310112834,
>    9.92726711556315, 11.5754703059793, 8.74886247329414, 8.99941809475422,
>    9.90840594749898, 11.1468604300171, 11.1322306562215, 10.49438144546,
>    9.50155213940889, 8.31737467087805, 5.76932597905397, 6.14411209244281,
>    7.39980584476143, 8.47632132936269, 8.00714262295514, 8.64454926922917,
>    7.79559868387878, 7.14818593114614, 7.42282171268016, 9.04718739911914,
>    12.0141573250294, 11.0411503817886, 11.7892528418452, 11.2668004352599,
>    10.5345542309806, 14.2355003859848, 12.4114783946425, 13.1144292186946,
>    14.3049817532301, 14.7282858844846, 9.90791183430701, 10.4058899218217,
>    12.0624131988734, 13.2521220948547, 13.9345653355122, 12.5256763771176,
>    12.3285478446633, 11.9927407242358, 11.6441268939525, 11.6448875516653,
>    30.5602320469916, 30.6964941322803, 27.3358505219221, 27.5474566966295,
>    24.3847575969994, 15.1250814087689, 15.0272130500525, 14.9795342702419,
>    14.2658210825175, 13.437497522682, 10.7001833617687, 10.0823557935655,
>    10.1298170629889, 9.99525294173509, 10.6919908896089, 9.04134479351342,
>    9.57930330187082, 9.58402880933136, 8.82056106347591, 9.06912200152874,
>    11.0435656271875, 12.827942892909, 14.6962288767099, 15.984565531835,
>    16.3673574104905, 17.7882182411849, 17.1887206379324, 16.4347139652818,
>    15.4833788517863, 14.3649869598448, 10.0324214436114, 10.9937381464988,
>    10.7803415972739, 10.64134365879, 10.3700830601156, 10.7242427766323,
>    10.1225153775886, 9.59254063200206, 9.67734202276915, 9.9705743137747,
>    6.15209711249918, 7.6417050557211, 9.55170588567853, 12.123644258827,
>    14.6793850231916, 13.8236853294075, 14.3564789090306, 13.6828002054244,
>    13.0476749036461, 12.3909330926836, 12.5938401091844, 12.5098232645541,
>    12.4792913440615, 10.5595408938825, 10.0890464382246, 9.20089432038367,
>    8.92592284362763, 8.59467086847872, 9.42603517323732, 10.0353622343391,
>    11.7311725392938, 12.4379832297564, 12.9343897104263, 12.9055073484778,
>    10.8944955747575, 13.6480727232993, 13.5285727679729, 13.1794585380703,
>    12.8222310449928, 12.3997843824327, 12.7413347829133, 14.3273916095495,
>    17.3931313678622, 18.2263168506324, 18.5841742437333, 6.59096706658602,
>    6.43405092414469, 6.25825286842883, 6.41100551001728, 6.47397979628295,
>    10.5375754879788, 11.7441980168223, 12.6210678834468, 13.6038213036954,
>    14.3639346119016, 14.6688716020435, 14.1826340463012, 15.2044224087149,
>    15.5630568042397, 15.0458208750933, 10.0154311163351, 9.7418615128845,
>    11.8866622913629, 10.4000290855765, 9.74880487192422, 12.071524746716,
>    11.5644979756325, 11.0723461490124, 10.6282578315586, 10.2157085202634,
>    14.5142644643784, 12.1188929770142, 12.3748247511685, 12.4087903182954,
>    11.9534945581108, 9.04913682024926, 10.3765605948865, 11.6044067312032,
>    11.8693192955106, 11.4852412138134, 9.60276927798986, 8.47671863157302,
>    6.53922976925969, 6.61022553686053, 6.93009907845408, 13.2296028546989,
>    13.0423339549452, 13.0597360432148, 12.6910961698741, 12.4157820828259,
>    10.1926731644198, 8.71818219311535, 7.08254557102919, 8.77621911931783,
>    10.0059285527095, 12.931788386777, 12.2630294412374, 11.4822425879538,
>    10.4378029704094, 9.7940765786916, 13.0133786704391, 11.9061049539596,
>    12.0638377033174, 12.3013137839735, 12.9490484017879, 13.2149957120419,
>    13.1087802350521, 12.6286820042878, 12.2278920840472, 11.8682594038546,
>    10.9492189250886, 12.2341319918633, 12.9464382771403, 12.5120461452752,
>    12.5263502821326, 12.6686599105597, 12.7322974149138, 12.1948833111674,
>    12.1215357910842, 11.9392029941082, 15.2677292469889, 16.3731585256755,
>    17.8960581310093, 18.6334447469562, 19.5818214677274, 8.80653981585056,
>    9.830889897421, 9.35642933472991, 8.49255602806807, 9.19627505354583,
>    9.56638909410685, 10.4608207242563, 11.0053240321577, 12.0839668437839,
>    12.6748947892338, 10.9087632503361, 11.0474556684494, 9.86553691327572,
>    11.7183218244463, 12.5948534812778, 9.51134513597935, 7.67265690956265,
>    8.47005187533796, 8.948102616705, 9.48919930960983, 8.92916852608323,
>    9.19180226046592, 9.93818349670619, 10.3347131051123, 9.19244724791497,
>    16.0914938896894, 16.6821955237538, 17.9938221350312, 19.0754321403801,
>    19.048942392692, 8.59134346246719, 8.39548541698605, 8.17942153662443,
>    8.02843223791569, 8.9953287737444, 7.97593365423381, 7.71139136049896,
>    7.85907462704927, 8.38070099707693, 9.28482818417251, 11.3056178670377,
>    11.601750086993, 11.2711317837238, 10.8186058234423, 10.7581429649144,
>    15.6826636288315, 16.9076268095523, 15.4331855010241, 15.1698420289904,
>    14.4226460717618, 11.3487603608519, 10.932231741026, 10.3945284616202,
>    9.96728525497019, 9.48596934322268, 10.508708213456, 10.0394641282037,
>    10.5090778553858, 10.1252990076318, 9.86525025218725, 21.985590364784,
>    22.3454732447863, 22.693102620542, 22.8635905310512, 23.2176823541522,
>    18.6908649746329, 16.1407203879207, 14.8633007425815, 13.0084274802357,
>    10.3990704054013, 6.98735397309065, 6.87530469149351, 8.9313744334504,
>    7.93048026971519, 8.05362006649375, 7.19595712143928, 6.09859018586576,
>    7.31170470826328, 8.58990701381117, 8.4448722191155, 10.6643167790025,
>    10.839969618246, 10.5106293456629, 10.4457534151152, 11.2185546196997,
>    12.6707960385829, 12.9902018699795, 12.9533659201115, 12.501154281199,
>    12.3501065187156, 25.9615670889616, 28.099115844816, 30.2258117124438,
>    32.2391155175865, 34.1092220507562, 13.0570391658694, 14.2825467512012,
>    11.1714780796319, 9.62660552468151, 13.1034480873495, 12.0462608523667,
>    12.1476030908525, 12.087664520368, 12.486698012799, 12.6554797869176,
>    12.9096878226846, 13.7426960282028, 15.2569429948926, 17.1046711038798,
>    17.0782153028995, 8.75586932525039, 8.82860643323511, 8.69223182089627,
>    9.15108947083354, 9.4462743261829, 8.55356580577791, 8.69411900639534,
>    8.9102350641042, 9.00506707839668, 8.75238287262619, 12.8364848904312,
>    14.6456281654537, 13.9498212374747, 14.5683591719717, 14.3893217202276,
>    15.1805742178112, 16.7262759525329, 17.7521643228829, 18.5243777465075,
>    18.8792126253247, 7.70680792629719, 7.47225251980126, 7.72799758706242,
>    7.68415729980916, 7.50800217501819, 9.68811193015426, 10.5253741610795,
>    10.922572016716, 10.9020531177521, 10.406608460471, 22.1927281469107,
>    21.7946967110038, 22.5350291468203, 22.0015277154744, 23.2784972526133,
>    25.1319196075201, 24.1645314730704, 23.0207713320851, 14.8746414575726,
>    12.5255933962762, 19.3960575386882, 19.3368871696293, 19.8454126249999,
>    19.8410699609667, 19.8172997217625, 12.1799279004335, 11.8857935070992,
>    11.4909932948649, 11.3612791523337, 10.8840802218765, 11.1973982769996,
>    11.6429010406137, 11.2867686431855, 11.5507948212326, 11.7122428491712,
>    13.8513946440071, 14.9497504346073, 14.425096521154, 13.2822252810001,
>    12.4311964027584, 18.864199379459, 17.5528808031231, 17.7616731729358,
>    17.1655979007483, 16.6251927148551, 29.3679255992174, 28.4771841019392,
>    27.9151875525713, 26.65377818048, 25.2528126351535, 10.6545137241483,
>    10.91169398278, 11.0310669522732, 11.1646522767842, 11.2674177624285,
>    13.7821182142943, 14.1553220339119, 15.0969068985432, 15.9642276819795,
>    16.6291657369584, 9.4556876225397, 9.84383365139365, 11.0380863770843,
>    10.6556000187993, 11.1149505246431, 8.38961955159903, 9.4479993218556,
>    10.1951210992411, 10.6412279885262, 10.8386783860624, 8.28430177643895,
>    8.50012865848839, 8.0173090333119, 8.15484160557389, 8.07647814508528,
>    10.3200965328142, 10.4913098970428, 10.3476996067911, 10.6061836704612,
>    12.1657092589885, 10.3872286621481, 9.38602960668504, 9.82730537652969,
>    9.79454554617405, 9.12395850755274, 12.1763132046908, 12.7074157353491,
>    12.6221365761012, 13.4234247263521, 15.5103187076747, 9.88674920517951,
>    9.41792191006243, 8.58000149019063, 7.98727499786764, 7.34257609583437,
>    13.8378750532866, 14.5356948953122, 14.5302697084844, 14.6059796679765,
>    14.1489790286869, 14.9558734148741, 15.146628767252, 15.4630133416504,
>    15.5585858970881, 15.4571908526123, 11.8359496816993, 11.2020426895469,
>    11.4698356948793, 11.8119870778173, 13.0321650300175, 17.7426278125495,
>    18.6734465416521, 18.8405636698008, 18.8715255819261, 18.9619445241988,
>    8.8628712343052, 8.674994437024, 9.01558804325759, 9.04601749498397,
>    8.85597188025713, 7.58305897470564, 7.92995095252991, 8.35649385116994,
>    9.23873609863222, 9.14969765581191, 12.9726023878902, 12.2728526126593,
>    13.0261426325887, 12.6654123421758, 11.5908016450703, 13.0077322013676,
>    12.6599280629307, 11.9994106236845, 10.1917257998139, 9.89739338401705,
>    10.7914459425956, 11.8336362764239, 11.7934257723391, 11.2242249771953,
>    11.4056261256337, 7.95377462636679, 7.26088020019233, 7.43080170359462,
>    7.50569254159927, 7.62218066956848, 11.2671461887658, 10.8180299866945,
>    9.43983325269073, 9.29652785416692, 10.826626047492, 14.3595944624394,
>    13.2217460777611, 12.7365244086832, 12.05212357454, 12.3027219437063,
>    13.1963438820094, 12.8045422956347, 13.7076315935701, 14.145736489445,
>    14.4983648322523, 14.3930621445179, 13.7241447810084, 13.0053710192442,
>    12.2289746068418, 11.4307265728712, 22.3180065862834, 17.3237380106002,
>    12.7182623371482, 13.0704908631742, 15.2839343994856, 11.1243085004389,
>    10.2472041500732, 10.5197993572801, 11.790946405381, 10.6045705731958,
>    15.1506495662034, 17.2426456119865, 18.0581725202501, 17.5418430939317,
>    16.011631116271, 16.6771751828492, 14.9888406973332, 14.0024574939162,
>    12.2754199896008, 10.462130815722, 14.700809167698, 14.7662508767098,
>    14.6368321962655, 13.8920741155744, 13.6426123324782, 7.52487180288881,
>    6.8714844295755, 7.11258086375892, 7.18187426682562, 7.26737848017365,
>    8.01721725147218, 9.51534896157682, 9.49199174065143, 9.66430208645761,
>    9.95999739971012, 12.6632636412978, 12.3405989259481, 12.1739520225674,
>    11.8746338412166, 11.4930238109082, 17.375064175576, 16.5855303872377,
>    14.6908791270107, 12.4465051107109, 10.6631374452263, 9.17110545560718,
>    8.15483720507473, 8.49230268504471, 9.13922635372728, 9.57141006365418,
>    16.033780714497, 17.3399481922388, 16.4341507013887, 15.3515323530883,
>    14.7840439807624, 18.8009101431817, 19.3318882025778, 20.5749990418553,
>    21.8101386912167, 21.9960610382259, 18.0659588892013, 17.8131891880184,
>    17.4943805672228, 17.3403216060251, 16.8955769855529, 12.620489532128,
>    12.2214950155467, 11.8860110174865, 11.3811555784196, 10.8314753975719,
>    13.4036011062562, 11.5633060690016, 11.6371187847108, 12.5311543699354,
>    13.4179203305393, 8.22134572081268, 7.50831649638712, 7.27005901280791,
>    7.60287002194673, 7.99200239125639, 7.90263516828418, 8.68863912764937,
>    10.4649641085416, 14.8291767574847, 13.2854715920985, 14.6683146245778,
>    15.3950218576938, 16.1753460299224, 18.3709637727588, 18.7799926847219,
>    9.85975402873009, 11.3263857085258, 14.0980262774974, 14.9891349021345,
>    15.565140126273, 17.7682626061141, 17.6397152245045, 18.1632375810295,
>    18.5020068660378, 18.6178280040622, 13.9469483401626, 13.3572864811867,
>    13.7237298768014, 15.0745737366378, 13.0753238685429, 7.80682750046253,
>    8.02811540197581, 8.54396957438439, 8.93615526147187, 9.23284823074937,
>    11.9208830874413, 11.34336409159, 9.64633170515299, 9.77506830822676,
>    9.60444209631532, 13.3866403251886, 13.6259520426393, 11.5198655985296,
>    10.6700826901942, 9.85463059041649, 16.529045579955, 14.2629016656429,
>    12.7639583777636, 13.6573225725442, 15.0617569684982, 9.50025964993984,
>    9.68771148473024, 9.27095026709139, 9.30016769561917, 9.69172285404056,
>    7.99956496339291, 7.4167326791212, 7.22712711431086, 8.56165643781424,
>    9.04990502167493, 16.1096038296819, 15.6424694694579, 16.1224633455276,
>    15.2468092739582, 15.2601830195636, 14.6924834232777, 15.2172856964171,
>    15.6576700508595, 15.8558295574039, 15.6930990982801, 10.0672576809302,
>    10.4989007581025, 10.7346505858004, 10.9321122989058, 10.1002658251673,
>    7.57602006196976, 8.28179977834225, 9.00425424333662, 8.75011347234249,
>    9.78429929818958, 8.22318575810641, 7.62580542359501, 7.52632019575685,
>    7.3945076437667, 8.00606575794518, 9.82791453134269, 10.3108039358631,
>    10.8194808941334, 11.0586643684655, 12.7866649534553, 16.4375944063067,
>    16.122004436329, 15.8343450631946, 15.183718688786, 14.59901179187,
>    13.086870778352, 13.8396339956671, 13.0286106839776, 12.6303931698203,
>    11.8594408035278, 12.4039673712105, 9.90002802573144, 9.60356576833874,
>    11.081666406244, 11.0487984493375, 15.9987502265722, 14.9749074596912,
>    13.8462209142745, 12.3910789377987, 11.7417626548558, 10.7962236274034,
>    11.77659323439, 11.0980827827007, 10.4603781597689, 10.4605271480978,
>    12.797769298777, 11.2864379771054, 9.58062659483403, 9.57864196971059,
>    9.7400170750916, 15.1035780552775, 15.3101249132305, 15.6179285142571,
>    14.4825984723866, 11.6881796624511, 11.791490809992, 11.2104086671025,
>    8.8539243908599, 8.34417999722064, 8.39954141993076, 9.41099112387747,
>    8.93235134426504, 9.60718737915158, 9.41101815551519, 9.83936337288469,
>    13.6638214811683, 14.4527215976268, 14.7365185897797, 13.2517122197896,
>    11.0009524505585, 9.60110148880631, 8.54964307509363, 8.75000974629074,
>    8.88564947526902, 7.84255138132721, 11.6202082950622, 12.075385870412,
>    12.8382677212358, 14.9491381365806, 20.0978868640959, 8.93126882147044,
>    9.09663643687963, 9.05409744009376, 8.98246862925589, 8.80278556142002,
>    8.68155935313553, 8.91096869017929, 7.71334832534194, 9.87222944386303,
>    11.2759735900909, 17.2249065712094, 17.9082475136966, 17.6210721954703,
>    16.7172310408205, 16.2506423424929, 12.9267014097422, 14.7103695664555,
>    19.504395313561, 22.4196153692901, 22.2453631460667, 8.23867111466825,
>    8.10000761412084, 7.8771845670417, 7.56322089582682, 7.14911003597081,
>    9.50618146453053, 8.6958515457809, 7.36113237217069, 6.79777669720352,
>    6.69330381788313), .Dim = c(10L, 90L), .Dimnames = list(NULL,
>    c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10",
>    "X11", "X12", "X13", "X14", "X15", "X16", "X17", "X18", "X19",
>    "X20", "X21", "X22", "X23", "X24", "X25", "X26", "X27", "X28",
>    "X29", "X30", "X31", "X32", "X33", "X34", "X35", "X36", "X37",
>    "X38", "X39", "X40", "X41", "X42", "X43", "X44", "X45", "X46",
>    "X47", "X48", "X49", "X50", "X51", "X52", "X53", "X54", "X55",
>    "X56", "X57", "X58", "X59", "X60", "X61", "X62", "X63", "X64",
>    "X65", "X66", "X67", "X68", "X69", "X70", "X71", "X72", "X73",
>    "X74", "X75", "X76", "X77", "X78", "X79", "X80", "X81", "X82",
>    "X83", "X84", "X85", "X86", "X87", "X88", "X89", "X90")))
>
> Is there any way to compute the means in this way? I just tried this, but I received the following error:
> result <- rowMeans(cbind(c(subset), c(subset5)));dim(result) <- dim(subset);colnames(result) <- colnames(subset)
>
> Error in rowMeans(cbind(c(subset), c(subset5))) : 'x' must be numeric
>
> Thanks,
> -----Original Message-----
> From: Eric Berger <[hidden email]>
> To: rain1290 <[hidden email]>
> Cc: r-sig-geo <[hidden email]>; R mailing list <[hidden email]>
> Sent: Fri, Apr 12, 2019 11:47 am
> Subject: Re: [R] Creating a mean line plot
>
> I don't have your data. Are the x-values the same in both plots?Does this example cover the situation?
> f1 <- function(x) { x^3 - 2 }f2 <- function(x) { 2 - x^2 }
> xV <- seq(from=0,to=2,length=50)y1 <- f1(xV)y2 <- f2(xV)y3 <- .5*(y1+y2)plot(x=xV,y=y1,col="blue",lwd=2,type='l',xlab="x",ylab="y")lines(x=xV,y=y2,col="green",lwd=2)lines(x=xV,y=y3,col="red",lwd=2)legend("topleft",legend=c("y1","y2","mean"),col=c("blue","green","red"),lwd=rep(2,3))
>
>
> On Fri, Apr 12, 2019 at 5:34 PM rain1290--- via R-help <[hidden email]> wrote:
>
> Hi there,
> I am trying to create a mean line plot that shows the mean of a series of separate line plots that correspond to two climate models. Let's first try getting the mean of two line plots. To create the separate line plots, here is what I did to set up the x and y axis variables:
>
> ####Getting cumulative emissions data for x-axis: 1-dimensional ####
>
> #For CanESM model#
>
> ncfname <- "cumulative_emissions_1pctCO2.nc"
> Model1 <- nc_open(ncfname)
> get <- ncvar_get(Model1, "cum_co2_emi-CanESM2")    #units of terratones of carbon (TtC) for x-axis (140 values)
> #For IPSL LR Model#
> #Getting cumulative emissions data for x-axis IPSL LR 1pctCO2 IPSL <- ncvar_get(Model1, "cum_co2_emi-IPSL-CM5A-LR")    #units of terratones of carbon (TtC) for x-axis (140 values)
>
> ############################################################################################################
>
> #####Getting precipitation data for y-axis - these are 3-dimensional####
>
> #For CanESM2 model#
> Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax")
>
>
> #For IPSL LR Model#
> Model10 <- brick("MaxPrecIPSLIPSL-CM5A-LR1pctCO2.nc", var="onedaymax")
> #############################################################################################################
> To create plots for a specific location:
> lonlat <- cbind(103,3)          #specifies a specific longitude and latitude
> Hope2 <- extract(Model2,lonlat)      #CanESM2
> Hope6 <- extract(Model10,lonlat)  #start IPSL CM5A LR
> plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative CO2 emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="One-day maximum precipitation for random location for 1pctCO2 scenario")
> lines(IPSL, Hope6, type="l", lwd="3", col="green")
> #############################################################################################################
> So, the idea would be to create a plot that shows the mean of these two plots. Given what I showed above, how should I go about creating the mean of these two green line plots? Would you have to get the mean of the x-values, and then obtain the mean of the y-values, and then plot these?
> Thanks, and any help would be greatly appreciated!
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.