R/indicatorsplot.R

Defines functions indicatorsplot

Documented in indicatorsplot

#' indicatorsplot
#'
#' Allows plotting the calculated indicators over the different datasets.
#' @author Beat Hulliger - Juan Berdugo
#' @param reportdataset (mandatory): A dataframe containing the report to be plotted.
#' @param indicator (mandatory): A string  containing the name of the indicator to plot.
#' @param variables (optional): A vector containing the names of the variables to be plotted (all variables are plotted by default)
#' @param datasets (optional): A vector containing the names of the datasets to be plotted (Datasets d1, d2, d3, d4 are plotted by default)
#' @return A a plot showing the datasets on the x axis and the value of the indicators on the y axis.
#' @export


indicatorsplot <- function(reportdataset,indicator,variables="allvars",datasets="alldatasets")

{

  #reportdataset <- read.table("T:/A1915_ICC/A1915_Projekte/A1915_SDAP/R-Funktionen/report5.csv", header=TRUE, sep=";", na.strings="NA", dec=".", strip.white=TRUE)
  #variables <- c("rentnet","statusinemployment")
  #datasets <- c("d1","d2","d3","d4")
  #indicator <- "imror"
  variablestoplot <- variables
  datasetstoplot <- datasets

  if (missing(indicator))
  {
    cat("Please specify the indicator to plot")
    break
  }else{indicatortoplot <- indicator}



  plot.new()
  par(mar=c(4,4,2,10)+0.1)
  par(xpd=TRUE)



  # Set the plot title
  graphtitle <- toupper(indicatortoplot)

  if (substr(indicatortoplot,1,1)=="w")
  {
    withweight <- "yes"
    graphtitle <- paste(graphtitle, " per dataset,", "with weighting")
  }else
  {
    withweight<- "none"
    graphtitle <- paste(graphtitle, " per dataset,", "without weighting")
  }


  datatoplot <- subset(reportdataset, indicator == indicatortoplot, select=c(value,dataset,scope))

  if (variablestoplot[1] == "allvars")
  {
    linestoplot <- unique(datatoplot[,3])
  }else
    {
      datatoplot <- datatoplot[which(datatoplot[,3] %in% variablestoplot),]
      linestoplot <- unique(datatoplot[,3])
    }

  if (datasetstoplot[1] != "alldatasets")
  {
    datatoplot <- datatoplot[which(datatoplot[,2] %in% datasetstoplot),]
  }


  if(nrow(datatoplot)==0)
  {
    return(cat("No results for plotting. Check the chosen indicator, datasets or variables"))

  }

  graphrange <- range(datatoplot[,1])

  xelements <- unique(datatoplot[,2])

  scopes <- as.vector(unique(reportdataset[,4]))
  linescolors <- c("orange","gray48","darkmagenta","green","red","blue")

  if (length(scopes)==length(linescolors))
  {matcolors<-as.data.frame(cbind(scopes,linescolors))}else
  {cat("Insufficient number of colors. Please declare more colors inside the function")}




  for (i in 1:length(linestoplot))
  {
    yvalues <- which(as.vector(datatoplot[,3]) %in% as.vector(linestoplot[i]))
    yvalues <- datatoplot[yvalues,1]
    if (i==1)
    {
      plot(c(1:length(xelements)),as.vector(yvalues),type= "o", xlab="Dataset", ylab= toupper(indicatortoplot), xlim=c(1,length(xelements)), col = as.character(matcolors[matcolors$scopes == linestoplot[i],2]), ylim=graphrange, axes = FALSE, lwd = 2)
      title(main=graphtitle)
      axis(1, at=1:length(xelements), lab=xelements)
      axis(2, t=5*0:graphrange[2])
      linesshown <- as.character(linestoplot[i])
      linescolors <- as.character(matcolors[matcolors$scopes == linestoplot[i],2])
    }else
    {
      lines(yvalues,type = "o", col = as.character(matcolors[matcolors$scopes == linestoplot[i],2]), lwd = 2)
      linesshown <- cbind(linesshown, as.character(linestoplot[i]))
      linescolors <- cbind(linescolors, as.character(matcolors[matcolors$scopes == linestoplot[i],2]))
    }
  }
  box()
  legend(length(xelements)+length(xelements)/25, max(graphrange),cex=0.65, as.vector(linesshown), as.vector(linescolors), lty=1)

return(cat("Plot Successful. Check plot screen"))


}

Try the sdap package in your browser

Any scripts or data that you put into this service are public.

sdap documentation built on May 2, 2019, 6:52 p.m.