R/PlotCor.R

Defines functions PlotCor

Documented in PlotCor

#' Plot correlation between Ref and ToComp
#'
#' Regression of ToComp onto Ref (ToComp = a + bRef)
#' Plot of ToComp as a function of Ref
#' Disp some informations on the plot (title, eq)
#'
#' @param Ref       Datas of reference system
#' @param ToComp    Datas of system to compare
#' @param U         "%"
#' @param RefT      Name of reference system
#' @param ToCompT   Name of system to compare
#' @param Cond      Main title
#' @param V         Current Variable
#' @param VT        Current Variable Name
#' @param UnitCurr  Unit to use
#' @param RefTUnit  Reference system Unit
#' @param Sym.Main
#'
#' @examples
#'  V <- Variables [iVariables]
#'  VT <- VariablesNames [iVariables]
#'  U <- "%"
#'  RefT <- RefNameToRead
#'  ToCompT <- ToCompNameToRead
#'  UnitCurr <- VariablesUnit[iVariables]
#'  Ref <- subset(D, App == RefName)[,V]
#'  ToComp <- subset(D, App == ToCompName)[,V]
#'  Cond <- paste(ToCompT, "VS", RefT)
#'  RefTUnit <- "1"
#'
#' @export

PlotCor <- function(Ref,
                    ToComp,
                    U,
                    RefT,
                    ToCompT,
                    Cond,
                    V,
                    VT,
                    UnitCurr,
                    RefTUnit,
                    Sym.Main = "") {

  #################

  par.pty = par()$pty     # save current pty (to restore it in the end)
  par(pty = "s")          # make axis square

  #################

  # Get the regression of ToComp onto Ref, ICC and B&A
  ToCompRef = lm(ToComp ~ Ref)                # plot(ToCompRef) for diagnostics
  sm = summary(ToCompRef)                     # print(sm) for details

  slope = ToCompRef$coefficients[2]
  intercept = ToCompRef$coefficients[1]
  rsquared = sm$r.squared

  #library(irr)
  i = icc(cbind(Ref, ToComp))

  #library(BlandAltmanLeh)
  ba = bland.altman.stats(ToComp, Ref)

  # Display some information in the consols
  cat(sprintf("\n--- %s : %s \n", Cond, V))
  cat(sprintf("Regression   :  %s = %0.2f*%s%+0.2f (r² = %0.2f)\n", ToCompT, slope, RefT, intercept, rsquared))

  #################

  # Plot Regression of M onto Z
  par(pty = "s")  # make axis square
  MaxX = max(Ref)
  MinX = min(Ref)
  MaxY = max(ToComp)
  MinY = min(ToComp)

  #if ToCompT name contains "Delta" then replace it by greek letter
  #Delta means difference End - Beg
  if (grepl("Delta", ToCompT, fixed = TRUE)) {
    newToCompT <- gsub("Delta","", ToCompT, fixed=TRUE)
    Ylabel = bquote(Delta ~ .(newToCompT) ~ .("(") * .(UnitCurr) * .(")"))
  } else {
    Ylabel = bquote(.(ToCompT) ~ .("(") * .(UnitCurr) * .(")"))
  }

  #if Cond name contains "Delta" then replace it by greek letter
  #Delta means difference End - Beg
  if (grepl("Delta", Cond, fixed = TRUE)) {
    newCond <- gsub("Delta","", Cond, fixed=TRUE)
    Cond = bquote(bold(Delta ~ .(newCond)))
  }

  plot(
    Ref,
    ToComp,
    col = "blue",
    xlab = bquote(.(RefT) ~ .("(") * .(RefTUnit) * .(")")) ,
    ylab = Ylabel
  )


  # add the equation of the regression line
  Equation = sprintf(
    "%s =\n %+0.2f %s %+0.2f\nr²=%0.2f",
    ToCompT,
    slope,
    RefT,
    intercept,
    rsquared
  )

  text(
    x = MinX + (MaxX - MinX) * .04,
    y = MinY + (MaxY - MinY) * .9,
    labels = Equation,
    adj = 0         # 0 = left ; 1 = right ; 0.5 = center
    #col = "blue"
  )

  title(Cond)

  abline(ToCompRef, col = "blue", lty = 2)    #regression line
  # abline(c(0, 1), col = "black", lty = 3)   #identity line


  #################

  #Add a blank plot to create newline
  plot.new()

  #################

  par(pty = par.pty)  # restore original pty
}
gfaity/ReachStroke documentation built on May 26, 2019, 10:34 a.m.