R/mixPlotYield.R

Defines functions mixPlotYield

Documented in mixPlotYield

#' mixPlotYield
#' @param dfData a data.frame object. Note that this is not a character string but the data.frame object itself. The parameter passed to the function should be without quotes (" ").
#' @param varYield a character string indicating the name of the column from the data.frame object \code{dfData} to be used as the y-variable for the plot. It is a continuous variable.  
#' @param varX a character string indicating the name of the column from the data.frame object \code{dfData} to be used as the x-variable for the plot. It is a continuous variable.
#' @param varTreat a character string indicating the name of the column from the data.frame object \code{dfData} to be used for color-coding the fill of the data points. It serves as the first legend for the plot. It is a categorical variable. 
#' @param varIndex a character string indicating the name of the column from the data.frame object \code{dfData} to be used for color-coding the boundary of the data points. It serves as the second legend for the plot. It is a continuous variable. 
#' @param ylab a character string indicating a label for the y-axis.
#' @param legend.controlTreat a list comprising of legend values and titles for the first legend. (Refer to the examples) 
#' @param legend.controlInd a list comprising of legend values and titles for the second legend. (Refer to the examples)
#' @param subDir a character string indicating the name of the subdirectory within the "plot" directory to save the output data.frame object (as a .txt file) and plot (as a .png file)respectively. If a subdirectory with the given name does not exist within output and/or plot, then it is created. If not specified, the outputs are stored in output/ and plot/. 
#' @param main a character string indicating an overall title for the plot.
#' @param width a number (default: 2400) indicating the width of the output plot.
#' @param height a number (default: 1100) indicating the height of the output plot.
#' @param res a number (default: 160) indicating the resolution of the output plot. 
#' @return mixPlotYield returns a plot of the yeild as a function of the organic matter using \code{varYield} as the yield variable and \code{varX} as the x-variable from the data.frame object \code{dfData} with the fill and the boundary color-coded using \code{} and \code{} columns from \code{dfData}. It saves the plot as a .png file in the \code{subDir} subdirectory within the directory "plot" inside the current working directory. It creates a "plot" directory in the current working directory if not present already. Similarly, if \code{subDir} is specified, it creates a subdirectory with the name \code{subDir} within plot/ if not already present, and saves the plot inside the subdirectory. If a subdirectory is not specified (i.e. missing \code{subDir}), then it saves the plot in plot/.
#' @description mixPlotYield takes as input a data.frame object (\code{dfData}), four character strings indicating the column names from \code{dfData} to be used respectively as the y-variable, the x-variable, the first legend, the second legend for the plot. The legend variables are used to color-code the fill and the boundary of the data points in the plot. Other inputs include a character string for the overall title of the plot (\code{main}), a character string indicating the name of the output subdirectory (\code{subDir}), three positive numbers indicating the width (\code{width}), height (\code{height}) and resolution (\code{res}) of the output plot. It returns a plot of the yield as a function of the organic matter from the data points using \code{varYield} and \code{varX} as the y and the x variables respectively. The plot is saved as a .png file in the \code{subDir} subdirectory within the directory "plot" inside the current working directory. A "plot" directory is created in the current working directory if not present already. Similarly, if \code{subDir} is specified, a subdirectory with the name \code{subDir} is created within  plot/ if not already present, and the plot is saved in that subdirectory. If a subdirectory is not specified (i.e. missing \code{subDir}), then the plot is saved in plot/.
#' @examples
#' load("./data/dat0indD_2018.rda")
#' mixPlotYield(dat0indD_2018,"yield","orgC","treatment",
#'             "indD",main="Rice yields",xlab="Organic matter (%)",
#'             legend.controlTreat=list(legend=c(1,2,3,6,7,8,10),title="Treatments"),
#'             legend.controlInd=list(legend=paste("Int",1:5,sep=""),title="Index Intensity"))
#' @export
mixPlotYield<-function(dfData,varYield="yield",
                       varX,varTreat,varIndex,ylab=expression(Yield~(Mg~ha^-1)),
                       ...,legend.controlTreat=NULL,legend.controlInd=NULL,subDir,width=2400,height=1100,res=160)
{
  
  par(mfcol=c(1,1),mar=c(5.1,5.1,4.1,2.1))
  # colVec<-gray(seq(0.70,0.05,length.out=7))
  colVec<-terrain.colors(length(levels(dfData[,varTreat])))[length(levels(dfData[,varTreat])):1]
  
  with(dfData,plot(get(varX)[order(get(varX),get(varYield))],get(varYield)[order(get(varX),get(varYield))],
                pch=21,cex=3,
                bg=colVec[get(varTreat)[order(get(varX),get(varYield))]],
                ylab=ylab,
                col=c("grey","grey50","hotpink",
                      "darkviolet","palevioletred4")[convertToLevels((get(varIndex)[order(get(varX),get(varYield))]),5,5)],
                lwd=3,...))
  
  
  
  with(dfData,text(x=get(varX)[order(get(varX),get(varYield))],
                y=get(varYield)[order(get(varX),get(varYield))],
                labels=round(get(varIndex)[order(get(varX),get(varYield))],2),col="black",cex=0.7))
  
  do.call("legend",c(list(x = "topleft",
                          fill=colVec,horiz=T,cex=0.75,x.intersp=0.5,y.intersp=0.75,
                          title.adj=0.5),legend.controlTreat))
  
  par(xpd=T)
  do.call("legend",c(list(x = "bottomright",
                          fill=c("grey","grey50","hotpink",
                                 "darkviolet","palevioletred4"),
                          horiz=T,cex=0.75,x.intersp=0.5,y.intersp=0.75,
                          title.adj=0.5),legend.controlInd))

  if (!any(dir(getwd())=="plot"))
  	{
    	print("directory plot has been created in the present working directory")
    	dir.create("plot/",recursive=T)
    }

    # test or add subDir als directory
    if(missing(subDir))
    {
      dirPlot<-file.path(getwd(),"plot")
    }
    else
    {
      dirPlot<-file.path(getwd(),"plot",subDir)
      if(!dir.exists(dirPlot))
      {
        dir.create(dirPlot)
      }
    }

  dev.copy(png, file=paste(dirPlot,"/",deparse(substitute(dfData)),".png",sep=""),width=width,height=height, res=res)
  dev.off()
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.