# [R] Creating a mean line plot

Eric Berger er|cjberger @end|ng |rom gm@||@com
Fri Apr 12 17:47:07 CEST 2019

```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 <r-help using r-project.org>
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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help