Plotting with R Statistical Package



  • Executing an external .R file:

    • input.R:

      • postscript(file="plot.ps")

    • R <- input.R --no-save

  • Reading into an array from the file:

tmp <- scan("tmp.txt",what=character(0));

wholeArray <- scan("tmp2.txt",what=double(0));


  • Reaching into an element in a matrix:

print( wholeArray[ X, Y] );


  • For Assignment:

a <- 3 ;

b <- c(1:10);


  • String concatenation:

str <- paste( str1, 3 , “ali”, sep=” ” )


  • Array functions:

    • length:

    • 1D / 2D / 3D array 0 is default values

nodeCount <- length(nodeNameList);

borderArray <- c(2,3);

dataArray <- array(0,c(stepNumber,nodeCount));

dataArray <- array(0,c(trial, guess, generation));


  • for loop:

for (i in 1:stepNumber){

for (j in 1:nodeCount){

dataArray[i,j] <- wholeArray[(i-1)*nodeCount+j];

}

}


  • Plot without nothing (which you only determine limits..)

par(lab=c(nTicksforX, nTicksforY, garbage));

plot(0,lty=0,xlim=c(0,1),ylim=c(-1,1),ylab="Y”,xlab="X",main="Title");


  • Boxplot:

    • for multiple box-and-whisker plots in one plot, you should have 2D array (matrix) such that, first index should be trials and second index should be features:

boxplot(data.frame(sData),col=rgb(1,0,0),ylim=c(0,X));

You can also use x<-list(c(3:7),c(8:10))

for( i in 1:dataCount ){

#inside plotting area

text(streamNames[i],x=i,y=0.85,srt=90); #rotate text 90 degrees.

#outside plotting area

mtext(side=1,names[i],at=i,adj=1); #should do par(las=2)

}


  • User-defined functions:

FunctionName= function(x,y) {

z <- x+y;

z

}


  • Plot borders

    • len = length(tmpArray);

min = 10000;
max = -10000;
for (i in 1:len){
if (tmpArray[i]<min)
min = tmpArray[i];
if (tmpArray[i]>max)
max = tmpArray[i];
}
yBorder <- c(min,max);
xBorder <- c(0,len);

  • Layout form:

    • print(nodeCount)

nf <- layout(matrix(1:(nodeCount),(nodeCount),1,byrow=TRUE), TRUE)
op <- par(lab = c(10,1,7));
for (i in 1:nodeCount){
    par(mar=c(.2,4,.2,.2));
# with color
    #barplot(dataArray[,i],ylim=c(-1,1),col=nodeNameList[i+nodeCount+1],border=nodeNameList[i+nodeCount+1],space=0,ylab=nodeNameList[i+1],cex.lab=1.5);
# no color
#barplot(dataArray[,i],ylim=c(-1,1),col=1,space=0,ylab=nodeNameList[i+1],cex.lab=1.5);
grid(nx=NULL,ny=NULL,lty=1,equilogs=true);
}
par(op);

  • placing Legends:

for(i in 0:4){

text(mds.dim1[number[gene.phase==i]], mds.dim2[number[gene.phase==i]],

gene.phase[number[gene.phase==i]] , cex=0.8, col= i+1)

}

legend(0.8, 1.0, phase.name, pch="01234", col=c(1,2,3,4,5))