PLOTTING BASICS topics: *basic plots *maps *images *printing graphs To open the graphics window In R: >X11() To close the graphics window: > dev.off() The following commands create a simple scatterplot; the semicolon is used to put more than one command on the line. > X11(); plot(1:10,1:10) View plot Arguments which add titles or modify the appearance of the plot can be included in the function: > plot(1:10,1:10,type="l",main="Straight line") View plot > hist(rnorm(100), main="Histogram of 100 random normal data points") View histogram >dev.off() Graphical Methods Plot plot(x,y,...) > xl<-"April 1975 - Dec 1975" > X11(); par(mfrow=c(3,1)) > read.table("climate")->climate > plot(climate[ ,1], xlab=xl, main="plot", type='l') > plot(lowess(climate[ ,1],f=1/3),xlab=xl, main="plot using lowess", type='l') # f is the fraction of the data used for smoothing at each x point. # The larger the f value, the smoother the fit. > dev.off() View plot When only one variable is specified in the arguments to plot(), the values of the variable are plotted against their indices, or against time in the case of time series data. > X11(); plot(climate[ ,1], climate[ ,2]) > abline(lsfit(climate[,1],climate[,2])) View plot Barplot barplot(x,names=NULL,...) Creates a bargraph. Several options are available including verticle or horizontal bars and shading patterns. names=NULL character vector of names for the bars > grades<-c(10,14,20,10) > grade.names<-c('Poor','Fair','Good','Excellent') > barplot(grades, names=grade.names, main="Barplot of exam grades") View barplot Pie pie(x, names=NULL, explode=F, ...) Creates a pie chart from a vector of data. names=NULL vector of slice labels > pie(grades, main="Pie chart of exam grades") View pie Dotchart dotchart(x, labels= , groups=NULL, pch="o",...) Creates a dot chart from a vector of data. A grouping variable, and a group summary may be used along with other options. labels= vector of labels for the data values groups=NULL categorical variable used for splitting data into groups grades<- c(10,14,20,10,15,16,15,8,5,10,25,14) grade.names<-c('Poor','Fair','Good','Excellent') grade.group<-factor(c(1,1,1,1,2,2,2,2,3,3,3,3), labels=c("Test 1","Test 2", "Final")) dotchart(grades, labels = grade.names, group = grade.group) title(main = "Distribution of marks by test") View dotchart Hist hist(x, nclass= , breaks= , probability=F,...) Creates a histogram. The same options available in barplot() are available in hist(). nclass= specifies the number of classes (ie.: bars) breaks= vector of the break points for the bars of the histogram probability=F if TRUE, the histogram will be scaled as a probability density > hist(rt(50,5), main="50 observations from a t distribution with 5 df") # random simulations from t distribution View histogram Density density(x, n=50, na.rm=F,...) Returns x and y coordinates of an estimate of the probability density of the data. n=50 number of equally spaced points at which to estimate the density na.rm=F logical flag: by default missing values generate an error message width= width of the window used in the computation the larger the value, the smoother the density > normal<-rnorm(100) > ndens<-density(normal, width=0.9) > hist(normal, probability=T) > lines(ndens) View density plot Specifying probability=T scales the histogram as a probability density so that both the line and the histogram are in the same scale. The demo() function shows an example where the histogram was not scaled but rather the ylim= argument was specified to achieve a similar effect. Boxplot boxplot(...) Produces side by side boxplots from a number of vectors. The boxplots can be made to display the variability of the median, and can have variable widths to represent differences in sample size. boxplot(split(grades, grade.group)) View boxplot Pairs pairs(matrix, ...) Produces all pair-wise scatter plots. > pairs(climate) # Here is how to create a graph in postscript # and save it in your Unix account (command printgraph) X11() x<- seq( 0,5,,300) y1<- exp( -x) plot( x,y1) # plot x versus y1 dev.off() # turn off postscript device postscript(file="ex3.ps") x<- seq( 0,5,,300) y1<- exp( -x) plot( x,y1) # plot x versus y1 dev.off() # turn off postscript device #the postscript file is saved in your Unix directory # use #stat% ghostview ex3.ps to preview it. # Now make some changes # add in lines y3<- sin( 2*pi*x) plot( x,y3) lines(x, y3, lty=3) # here is a interesting plot y3<- sin( 2*pi*x)* exp( -x) y4<- cos( 2*pi*x)*y1 plot( y3,y4, type="l") # ind<- y1 > max(y1)*.8 # a logical variable indicating when the # exponential envelope is 80% of its maximum #value points( y3[ind], y4[ind]) # now indicate these polygon( c(0,y3[ind]), c(0,y4[ind]) ) # mark the area where # the exponential envelope is 80% of its maximum # Now some practice adding text ------------------------------------------------------ MAPS >library(fields) >US() points( climate$lon, climate$lat, pch="x") # use period for plot character text( climate$lon, climate$lat, climate$elev) # label by elevation ## xlim and y lim to indicate the limits of your usa graph US( xlim=c(-82, -73), ylim=c(32.5,41)) points( climate$lon, climate$lat, pch="o") # use period for plot character # label by city names but make the text 1/2 the default size. # left justify the label locator(1)-> hold # now click on a point in the plot with the left button hold # gives you the coordinates points( hold, pch="X") # text( hold$x, hold$y, "Here is my favorite place", adj=1) # adj=1 right justify the label ---------------------------------------------------------------- GRAPHS # Here are some methods for putting several plots on a page x<- seq( -3,3,,80) #creates a vector with values from -3 to 3 of length 80 y1<- dnorm( x) # N(0,1) density y2<- pnorm( x) # N(0,1) CDF plot( x,y1, type="l") plot( x,y2, type="l") # The problem is that the second plot erases the first par(mfrow=c( 2,1)) # divide page into a 2X1 panel plot( x,y1, type="l") plot( x,y2, type="l") title("Standard Normal CDF") # title wil go to the last plot drawn More examples: # Here is a way to view some histogram of random N(0,1) samples par(mfrow=c( 4,4)) for ( k in 1:16) { plot( rnorm(50))} # for loop will go 16 times #rnorm generates at random sample of N(0,1)'s par(mfrow=c(1,2)) # this is a pleasing aspect ratio for four plots set.seed(123) #fix seed so that we all see the same thing X<- matrix( rnorm(200*3), ncol=3) # X is a matrix of random N(0,1)'s boxplot( X[,1],X[,2], X[,3]) # SPLUS style boxplot title("boxplot function") hist( X[,1]) title( "hist function") # Options in par par( cex=.8) # make the character expansion size smaller pairs(X) # redraw ------------------------------------------------------------ IMAGES: # Using surface plotting x1<- 5:10 x2<- 1:6 outer( x1,x2,"+")-> f f par(mfrow=c(2,2)) image( x1,x2,f) persp( x1,x2,f) contour( x1,x2,f) # an important list for surface plots look<- list( x=x1, y=x2,z=f) image(look) ---------------- SPATIAL INTERPOLATION library(geoR) data(s100) #Loads specified data sets, or list the available data sets. loci <- expand.grid(seq(0,1,l=31), seq(0,1,l=31)) #create a 31x31 grid #interpolation at the "loci" locations": kc <- ksline(coords=s100$coords,data=s100$data, loc=loci,cov.pars=c(1,.5)) #spatial interpolation of our dataset s100 # at the locations in the grid "loci" kc$predict #predicted (interpolated) values kc$krige.var #variance of our interpolated values #image graph of the interpolated values and their #standard error at the "loci" locations: par(mfrow=c(1,2)) image.kriging(kc, loc=loci, main="interpolated values") image.kriging(kc, loc=loci, val=sqrt(kc$krige.var), main="std. errors") Printing --------- postscript("file.ps") plot((1:10), col=topo.colors(64)) dev.off() -------------------- COLOR: The color of the created postscript file will be the same as the one showed in the R window `col' A specification for the default plotting color. `col.axis' The color to be used for axis annotation. `col.lab' The color to be used for x and y labels. `col.main' The color to be used for plot main titles. `col.sub' The color to be used for plot sub-titles. Use colors() for additional information ------------------------ CONTROLING TICK INTERVALS ON AXES par(lab=c(...)) lab= controls number of tick intervals and length of labels x= number of tick intervals on x axis (default = 5) y= number of tick intervals on y axis (default = 5) llen= length of labels on both axes (default = 7) eg.: lab=c(x=6, y=4, llen=9) >par(lab=c(x=10,y=10,llen=10)) > plot(1:10,1:10) ------------------------------- Assignment: create a vector x with equally spaced values from -4 to 4 of length 100, divide the page into a 2X1 Panel, plot a boxplot of x and then a histogram of 50 simulated values from a N(0,1), add a density graph to the histogram.