[R] Plotting multiple columns on same graph

Landon Sego sego at stat.wisc.edu
Fri Feb 8 22:02:31 CET 2002


On Fri, 8 Feb 2002, Andrew Perrin wrote:

> I'd like to produce a series of simple line graphs for my methods class
> that show the three questions used on a repeated survey to make up a
> particular index.  The data frame is:
> 
> > efficacy.df
>    year complicated havesay dontcare
> 1  1952          71      68       63
> 2  1954          NA      NA       NA
> 3  1956          64      71       71
> 4  1958          NA      NA       NA
> 5  1960          59      72       73
> 6  1962          NA      NA       NA
> 7  1964          67      70       62
> 8  1966          69      60       57
> 9  1968          71      58       55
> 10 1970          73      64       50
> 11 1972          74      59       49
> 
> The ideal graph will show each of the three questions as a different
> color line, with lines connecting years spanned by NA values.  What's the
> easiest way to do this?
> 

Following is an example of such a graph.  Save the data below into a file
'loan.tab'.  Then run the example code that appears below the data.  By
the way, this example comes from an R package called "intrograph" (a
package of examples designed to introduce R graphics to new users) that
should be shortly making its debut on CRAN.

--Landon Sego

num adjmon  newrate oldrate fixedrate paid
 1  Jan-00  8.09091 7.33541     8.250 0.23
 2  Feb-00  8.20139 7.20833     8.375 0.17
 3  Mar-00  7.90457 8.03989     8.250 0.49
 4  Apr-00  8.22500 7.34167     8.250 0.20
 5  May-00  8.41346 7.35577     8.375 0.08
 6  Jun-00  8.75000 7.52273     8.500 0.27
 7  Jul-00  8.54348 8.70109     8.250 0.00
 8  Aug-00  8.61923 7.44231     8.125 0.08


## Example:  group.plotting

loan <- read.table(file='loan.tab',header=T)

attach(loan)
par(las=1)

# Notice how "type='n'" allows us to custom plot the lines and characters for
# different groups individually

plot(num,newrate,type='n',main='Comparison of Interest Rates',
     xlab='Adjustment Month',ylab='Average Interest Rate',ylim=c(6.7,9.2))

# Plot the fixed rate curve and points

points(num,fixedrate,pch='F')
lines(num,fixedrate,lty=1)

# Plot the new rate curve and points

points(num,newrate,pch='N')
lines(num,newrate,lty=3)

# Plot the oldrate curve and points

points(num,oldrate,pch='O')
lines(num,oldrate,lty=2)

# Replot the horizontal axis

axis(1,1:8,col.axis='white')
axis(1,1:8,labels=c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug'),cex=1.2)

# Add descriptive text

text(4.5,6.85,'Percentage of loans paid off:')
text(num,rep(6.7,8),c('23\%','17\%','49\%','25\%','14\%','27\%','0\%','20\%'))

# Add legend

legend(.8,9.25,c('Fixedrate','Newrate','Oldrate'),lty=c(1,3,2))

# Detach the loan data set from the search path 

detach()

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list