[R] Plots by subject

Thomas Lumley tlumley at u.washington.edu
Tue Sep 10 02:09:43 CEST 2002


On Mon, 9 Sep 2002, Tanya Murphy wrote:

> Hello! I need some help with creating plots for each study subject. I have
> tried the 'for' command as described in 'An Introduction to R', but I wasn't
> successful.
>
> Here's what I want to do:
> -Create and save the following overlayed scatterplots for each subject (1 to
> n).
> -The data frame is in the form of one line per visit per subject (so more than
> one line per subject). (I thought this may be why the 'split' and 'for'
> functions didn't do it for me.)
>
> This works one subject:
>
> pt <- ID == '1'
> par(oma=c(0,2,0,3))
> plot(VISITWK[pt], T4[pt], ylim=c(0,500), xlim=c(0,50), xlab='Visit week',
> type='b', ylab='CD4 count (cells/ul) [-o-]')
> par(new=T)
> plot(VISITWK[pt], LOG.BDUS[pt], ylim=c(1,6), xlim=c(0,50), pch='+', axes=F,
> ylab='', type='b', xlab='', main='Viral load and CD4 for ID=1')
> axis(4)
> mtext("Viral load (log10) [-+-]",4, outer=T)
> savePlot(filename='ID_1', type=c('pdf'))
>
> Is there something like a macro variable that I can use in the plot arguments
> and in the title and filename (or at least some way to save all the plots)?
>

It should work with a for() loop. It seems that your id variable is a
character string rather than a number, which might have been the problem
Something like:

for (patient in as.character(1:n)){
  pt <- ID == patient
  par(oma=c(0,2,0,3))
  plot(VISITWK[pt], T4[pt], ylim=c(0,500), xlim=c(0,50), xlab='Visit week',
  type='b', ylab='CD4 count (cells/ul) [-o-]')
  par(new=T)
  plot(VISITWK[pt], LOG.BDUS[pt], ylim=c(1,6), xlim=c(0,50), pch='+', axes=F,
  ylab='', type='b', xlab='', main=paste("Viral load and CD4 for ID=",patient))
  axis(4)
  mtext("Viral load (log10) [-+-]",4, outer=T)
  savePlot(filename=paste("ID",patient,sep="_"), type=c('pdf'))
}


	-thomas


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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