knitr::opts_chunk$set(echo = TRUE)

Description

Currently, the function "relation" is to develop for visualizing the behavior among one variable ,two variables as three variables. The sub-functions for one and two variables visualization from R basic packages, and sub-functions for three variables from package "car" and package "rgl".

relation <- function(x,y=NULL,z=NULL)
{
  if (is.null(x) && is.null(y) && is.null(z))
  {
    print("There is no variable, please check.")
  }

  if(is.null(y) && is.null(z))
  {
    x_names = deparse(substitute(x)) 
    print(paste("There is only one variables x:=",x_names))
    cat("Summary statistics:\n")
    summary(x)
    opar <- par(no.readonly = TRUE)
    par(mfrow=c(2,1))
    plot(density(x),main=paste("Density plot for",x_names))
    boxplot(x,main=paste("Box plot for",x_names))
    par(opar)
  }else if (!is.null(y) && is.null(z))
  {
    x_names = deparse(substitute(x)) 
    y_names = deparse(substitute(y)) 
    cat(paste("There are two variables\n","x:=",x_names,
                "\n y:=",y_names,"\n"))
    cat("Summary statistics:\n")
    summary(cbind(x,y))
    opar <- par(no.readonly = TRUE)
    par(fig=c(0,0.8,0,0.8))
    plot(x,y,xlab = x_names,ylab = y_names)
    lo <- loess(y~x)
    lines(predict(lo), col='red', lwd=2)
    par(fig=c(0,0.8,0.55,1),new=T)
    boxplot(x,horizontal = T,axes=F)
    par(fig=c(0.65,1,0,0.8),new=T)
    boxplot(y,axes=F)
    par(opar)

  }else if (!is.null(y) && !is.null(z))
  {
    x_names = deparse(substitute(x)) 
    y_names = deparse(substitute(y)) 
    z_names = deparse(substitute(z)) 
    cat(paste("There are two variables\n","x:=",x_names,
              "\n y:=",y_names,"\n z:=",z_names,"\n"))
    cat("Summary statistics:\n")
    summary(cbind(x,y,z))
    opar <- par(no.readonly = TRUE)
    par(fig=c(0,0.8,0,0.8))
    panel.cor <- function(x, y, digits=2, prefix="", cex.cor) 
    {
      usr <- par("usr"); on.exit(par(usr)) 
      par(usr = c(0, 1, 0, 1)) 
      r <- abs(cor(x, y)) 
      txt <- format(c(r, 0.123456789), digits=digits)[1] 
      txt <- paste(prefix, txt, sep="") 
      if(missing(cex.cor)) cex <- 0.8/strwidth(txt) 

      test <- cor.test(x,y) 
      # borrowed from printCoefmat
      Signif <- symnum(test$p.value, corr = FALSE, na = FALSE, 
                       cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
                       symbols = c("***", "**", "*", ".", " ")) 

      text(0.5, 0.5, txt, cex = cex * r) 
      text(.8, .8, Signif, cex=cex, col=2) 
    }

    pairs(data.frame(x,y,z), label = c(x_names,y_names,z_names),lower.panel=panel.smooth, upper.panel=panel.cor)
    par(opar)

    require(car)
    require(rgl)
    scatter3d(x,y,z,xlab=deparse(substitute(x)), ylab=deparse(substitute(y)),
      zlab=deparse(substitute(z)))
  }

}

Example

This example use iris data.

data(iris)
head(iris)

Single variables

Includeing density plot and boxplot

data(iris)
head(iris)

relation(iris$Sepal.Width)


ShouyeLiu/metaboliteUtility documentation built on May 6, 2019, 9:07 a.m.