R/makeplot.R

#' A plotting Function
#'
#' This function allows you to make plots.
#' @keywords makeplot
#' @examples
#' makeplot()

makeplot<-function(PC,PR,NCOL,NAME_COL,NROW,NAME_ROW,TITLE,NXL){
  #Choose the variant and PCs
  choice = as.integer(readline(prompt = "Which PCA variant do you want?  Type an integer from 1 to 7: "))
  while(!is.element(choice,c(1,2,3,4,5,6,7))){
    choice = as.integer(readline(prompt = "Please type an integer from 1 to 7: "))
  }
  target.PR = PR[,(abs(NXL*choice)-NXL+1):(NXL*abs(choice))]
  target.PC = PC[,(abs(NXL*choice)-NXL+1):(NXL*abs(choice))]
  if(sum(is.na(target.PR))==dim(target.PR)[1]*NXL & sum(is.na(target.PC))==dim(target.PC)[1]*NXL){
    print("Sorry but CA cannot be used with any negative data values. Do you want to make another graph?")
  }
  else{
    PC_choice1 = as.integer(readline(prompt = paste("Enter the PC you want for x-axis from 1 to",paste(as.character(NXL),", add a negative sign if you want to reverse the polarity: ",sep = ''))))
    while(!is.element(PC_choice1,(-NXL):NXL)){
      PC_choice1 = as.integer(readline(prompt = paste("Please type an integer from -",as.character(NXL),"to",paste(as.character(NXL),": ",sep = ''))))
    }
    PC_choice2 = as.integer(readline(prompt = paste("Enter the PC you want for y-axis from 1 to",paste(as.character(NXL),", add a negative sign if you want to reverse the polarity: ",sep = ''))))
    while(!is.element(PC_choice2,(-NXL):NXL)){
      PC_choice2 = as.integer(readline(prompt = paste("Please type an integer from -",as.character(NXL),"to",paste(as.character(NXL),": ",sep = ''))))
    }
    print("Graphing...")
    if(PC_choice1>0){
      PR1 = target.PR[,abs(PC_choice1)]
      PC1 = target.PC[,abs(PC_choice1)]
    }else if(PC_choice1<0){
      PR1 = target.PR[,abs(PC_choice1)]*(-1)
      PC1 = target.PC[,abs(PC_choice1)]*(-1)
    }
    #PC2 can also be both positive and negative
    if(PC_choice2>0){
      PR2 = target.PR[,abs(PC_choice2)]
      PC2 = target.PC[,abs(PC_choice2)]
    }else if(PC_choice2<0){
      PR2 = target.PR[,abs(PC_choice2)]*(-1)
      PC2 = target.PC[,abs(PC_choice2)]*(-1)
    }
    
    #Define the name of each choices
    if (choice==1){
      title="Grand Mean Centered"
    }else if(choice==2){
      title="Column Centered"
    }else if(choice==3){
      title="Row Centered"
    }else if(choice==4){
      title= "Double Centered (AMMI)"
    }else if(choice==5){
      title="Column Standardized" 
    }else if(choice==6){
      title= "Row Standardized" 
    }else if(choice==7) {
      title="Correspondence Analysis" 
    }
    #Make Plot
    par(mgp=c(2.5,1,0)) 
    plot(PC1,PC2,col = 'black', xlim = range(c(PC1,PR1)),ylim = range(c(PC2,PR2)),pch=17,cex.lab=1.7,cex.axis=1.3,
         xlab = paste('PC',as.character(abs(PC_choice1)),sep = ''),ylab = paste('PC',as.character(abs(PC_choice2)),sep = ''),
         main = paste(NCOL,NAME_COL,"(triangles)",NROW,NAME_ROW,"(circles)",TITLE, title ,sep = ' '),cex=0.6,asp=1)
    lines(PR1,PR2,col = 'red',type = 'p',pch=16,cex=0.6)
    abline(h = 0, v = 0, lty=2)
    print("Graph is ready, use the Export feature to save as image or pdf. Do you want to make another graph?")
    
  }
}
sq77/PCA7 documentation built on May 15, 2019, 4:49 p.m.