R/plot2Var.R

Defines functions plot2Var

Documented in plot2Var

#' plot2Var
#' @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 plotVar1 a character string indicating the name of the column from data.frame object \code{dfData} to be plotted.
#' @param orderVar a character string indicating the name of the ordering variable. It is one of the two variables used to create the plot - \code{plotVar1}, \code{plotVar2}, \code{plotVar3}
#' @param ylim1 a numeric vector containing two values indicating the lower and upper limits of the first y-axis.
#' @param scaleFVar3 a number indicating how much the third y-axis be scaled compared to the first two y-axes.
#' @param plotVar2 a character string indicating the name of the column from data.frame object \code{dfData} to be plotted.
#' @param secondAxis a character string indicating the label for the second y-axis.
#' @param secYaxStep a number indicating the difference between consecutive tick values for the second y-axis.
#' @param ylim2 a numeric vector containing two values indicating the lower and upper limits of the second y-axis.
#' @param subDir a character string indicating the name of the subdirectory within "output" and "plot" directories 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: 1000) indicating the width of the output plot.
#' @param height a number (default: 600) indicating the height of the output plot.
#' @param res a number (default: 125) indicating the resolution of the output plot.
#' @param copy a boolean variable (default: F) indicating whether to save the plot as a .png file or not.
#' @return plot2Var returns a plot of 3 variables (\code{plotVar1}, \code{plotVar2}, \code{plotVar3}) from the data.frame object \code{dfData} and saves the plot as a .png file in the "plot" directory within the current working directory.  If the directory called "plot" is not present in the current working directory, a new directory called "plot" is created. 
#' @description plot2Var takes as input a data.frame object (\code{dfData}), two column names (\code{plotVar1}, \code{plotVar2}), two character strings (\code{firstAxis}, \code{secondAxis})specifying the axis labels to be used respectively for the two y-axis, a character string indicating the name of the ordering variable (should be one of - \code{plotVar1}, \code{plotVar2}), a character string (\code{main}) for the overall title of the plot, 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, and a boolean variable indicating whether to save a copy of the plot as a .png file. It then generates a plot from two variables and for copy == T, saves the plot as a .png file in the "plot" directory within the current working directory.  If the directory called "plot" is not present in the current working directory, a new directory called "plot" is created. 
#' @examples
#' plot2Var(dfData=ca_s,plotVar1="yieldWs",
#'          orderVar="yieldWs",ylim1=c(0,14),scaleFVar3=0.3,
#'          plotVar2= "nFertWs",
#'          secondAxis=paste( "N-fert", " (in kg/ha)",sep=""),
#'          secYaxStep=40,ylim2=c(0,320),
#'          main="Ascending amount of N fertilizer vs. Yield",copy=T)
#' @export
plot2Var<-function(dfData,plotVar1,orderVar,ylim1,scaleFVar3,
                  plotVar2,secondAxis,secYaxStep,ylim2,subDir,
                  main=NULL,width=1000,height=600, res=125,copy=F)
{
  par(mar=c(5,4.5,3,4),xaxt="s",las=0)
  
  #plotVar
  plot(dfData[,plotVar1][order(dfData[,orderVar])],
       ylim=ylim1,
       main=main,
       xlab=paste("Farmers n=",length(dfData[,1]),sep=""),
       ylab=expression(Yield~(Kg~ha^-1)))
  abline(v=(c(1:length(dfData[,1]))),col="grey90")
  segments(1:length(dfData[,1]),0,1:length(dfData[,1]),dfData[,plotVar1][order(dfData[,orderVar])])
  abline(h=0)
  
  #plotVar2
  par(new=T,xaxt="n",yaxt="n",las=0)
  plot(dfData[,plotVar2][order(dfData[,orderVar])],col="brown",pch=20,xlab="",ylab="",
       ylim=ylim2)
  lines(dfData[,plotVar2][order(dfData[,orderVar])],col="brown",type="l")
  par(new=T,yaxt="s")
  axis(4,at=seq(0,max(dfData[,plotVar2]),secYaxStep),seq(0,max(dfData[,plotVar2]),secYaxStep))
  mtext(secondAxis,4,line=2)
  
  if(copy==T)
  {
    #test or add plot directory
  	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,filename=paste(dirPlot,deparse(substitute(dfData)),"_",
                            plotVar1,plotVar2,".png",sep=""),width=width,height=height, res=res)
    dev.off()  
  }
}  
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.