Plot: quick reference

Things I find myself needing often:

box()                                 draws a box around the figure

par(mar=c(4,4,4,4))           can reset the margins for (bottom, left, top, right)

par(cex=2)                        makes all of the axis labels larger

par(mfrow=c(3,4))             makes panes in the Figure (rows,columns)

legend(100,28,legend=c("Mean","95% PI","95% PI"),col=c("blue","red","green"),lwd=c(1,1,2), lty=c(1,2,3))

segments(x0,y0,x1,y1)       adds line segments to a plot

curve()                               plots a curve like dnorm

abline(3,2)                         (intercept, slope)   or v=1  or h=2  for vertical or horizontal lines

acf()

plot(density())

 

Writing plots to files:

postscript(file="file_name.ps", horizontal=FALSE, paper="special", height=6, width=6)        

pdf(file="file_name.pdf",width=7,height=3)                                                                    

bmp(), jpeg(), png(), tiff()  

dev.off()                            closes the writing of a plot to a window or file
 

 

Calling parts of a matrix.  If I have a matrix with elements A=matrix(1:12,4,3) and I want elements from rows 1:4 but columns v=c(2,1,3,2).  A[1:4,v] (or A[,v]) gives a matrix back, not a vector, which is usually not what I want.  A[cbind(1:4,v)] gives a vector back. 

 

Tags: