#' A Self-Defined Plot Function that Simplifies Plotting with multiple series
#' 
#' This function allows you to plot without much specification.
#' param x The x-axes to be plotted. Default value is NA so that x-axes is extracted from the first column of yseries_input
#' param yseries_input A data.frame object. The first column is x-axes and other columns are plotted.
#' yseries_input = MyPlot( yseries_input = data.frame(seq(1:10),rnorm(10),rnorm(10) ), LegendNames=c( "First Random Sequence", "Second Random Sequence" ) )
MyPlot <- function(  x=NULL,  yseries_input,  PlotScale=1.2, Linewidth=2, LegendPosition="topleft", LegendNames=NA, main="", xlab="", ylab="", xlim=NA, ylim=NA, colors=NA, cex.lab=1, cex.axis=1, cex.main=1.1,  mgp = c(2.2, 0.8, 0) ){
  # Note: x is a vector, and yseries_input is either a dataframe or a vector
  if(is.na(colors)){
    colors = c( "blue", "red", "purple", "azure4", "darkslategray4", "goldenrod2" )
  }
  yseries_input = as.data.frame(yseries_input)
  N.dim = ncol( yseries_input )
  if( (is.null(x) > 0) ){   # If there is no input from x and only one column is supplied from yseries_input
    if(  ncol(yseries_input)==1 ){
      x =  seq( 1:nrow(yseries_input) )
    }else{
      x = yseries_input[,1]
      yseries_input = yseries_input[ , 2:N.dim ]
    }
  }
  DT = cbind( x, yseries_input )
  DT = as.data.frame(DT)
  NumNAs =  apply( DT, 1, function(z) sum(is.na(z)))  # Eliminate NAs
  DT = DT[NumNAs<=0, ]
  DT = DT[ order(DT$x),   ]  # Order the table by x
  yseries = DT[ , 2:ncol(DT) ]
  N.dim = ncol(DT)
  if( sum(is.na(ylim))>0 | length(ylim)<2 ){
    ylim = c( min(yseries,na.rm=TRUE), min(yseries,na.rm=TRUE) + (max(yseries,na.rm=TRUE)-min(yseries,na.rm=TRUE))*PlotScale )  # y axes limit
  }
  if( sum(is.na(xlim))>0 | length(xlim)<2 ){
    xlim = c( min(DT[,1],na.rm=TRUE), max(DT[,1],na.rm=TRUE) )  # y axes limit
  }
  plot( DT[,1], DT[,2],  type="l", col=colors[1], xlab=xlab,  ylab=ylab, main=main, lwd=Linewidth, xlim=xlim, ylim=ylim , mgp = mgp, cex.lab=cex.lab, cex.axis=cex.axis, cex.main=cex.main)
  if( N.dim>2 ){  # More than 1 series
    for( i in 2:(N.dim-1) ){
      lines(DT[,1] , DT[,i+1],  lty=i, col=colors[i], lwd=Linewidth )
    }
  }
  if( sum(!is.na(LegendNames))>0 ){  # if legend name is provided
    legend( LegendPosition , legend = LegendNames,  bty = "n", lty= seq(1, N.dim-1) , col=colors[seq(1, N.dim-1)], lwd=Linewidth*rep(1,N.dim-1)  )
  }
}
