[R] Plots by subject

Tanya Murphy tmurph6 at po-box.mcgill.ca
Wed Nov 20 16:07:27 CET 2002

Thank you for your reply about the for () loop. The as.character advice 
worked. Sorry for the delay in getting back to—I had to set the project aside 
for a few weeks.

This didn’t work exactly as is
for (patient in as.character(1:n)){
  pt <- MRN == patient
(rest of the function)

But this did
for (patient in as.character(levels(MRN))){
pt <- MRN == patient
(rest of the function)

What I didn’t mention last time was that the whole intended result is a sheet 
(one per patient) with a few graphs and tables:

(Used > nf <- layout(matrix(c(1,1,2,2,3,4),3,2,byrow=TRUE), c(1,1),c(7,3,3), 
FALSE) to section the page.)

The data are from 4 different dataframes (df) (one for each plot) and there 
are a variable number of lines for any given patient across the df (e.g. every 
patient has one line in the demographics df, but a patient may have 3 lines in 
the genotype df, 2 lines in the drug df (3 tables are plotted with the data 
from these three df) and 14 lines in the lab df (for the graph you helped me 

So the whole function for one patient is:
# Overlay plot
lab <-read.xport('lab')
celld <- as.date(CELLDATE)
vld <- as.date(VIRDATE)
gtd <- as.date(GTDATE)
par(mar=c(2,6,1,6) + 0.1)
plot(celld, T4, ylim=c(0,800), pch=4, type='o', ylab='CD4 count (cells/ul) 
[-x-]', xlab=’’)
plot(gtd, Y, ylim=c(0,800), type='p', pch=8, axes=F, ylab='', xlab='', 
plot(vld, VL, axes=F, ylim=c(0,6), lty=5, ylab='', type='c', xlab='')
plot(vld, VL0, axes=F, ylim=c(0,6), pch=16, lty=2, ylab='', type='p', xlab='', 
plot(vld, VL1, axes=F, ylim=c(0,6), pch=1, lty=2, ylab='', type='p', xlab='', 
mtext("Viral load (log10) [--o--]",line=3, 4, outer=F, cex=0.7)

# Genotype table
gt <-read.xport('gt')
spd <- as.date(SPECDATE)
par(mar = c(0, 1, 1, 1) + 0.1) 
text(0,1,paste(“GT date”), adj=c(0,1) , font=2)
text(0.15,1,paste(“Duplicate”), adj=c(0.5,1) , font=2)
text(0.3,1,paste(“PI mutations”), adj=c(0.5,1) , font=2)
text(0.7,1,paste(“RT mutations”), adj=c(0.5,1) , font=2)
text(0,0.85,paste(spd, collapse='\n\n'), adj=c(0,1))
text(0.15, 0.85,paste(DUP, collapse='\n\n'), adj=c(0.5,1))
text(0.3, 0.85,paste(PIMUT, collapse='\n\n'), adj=c(0.5,1))
text(0.7, 0.85,paste(RTMUT, collapse='\n\n'), adj=c(0.5,1))

# Heading table
bl <-read.xport('bl')
dob <- as.date(DOB)
fad <- as.date(FIRSTARV)
fvd <- as.date(FIRSTVIS)
lvd <- as.date(LASTVISI)
par(mar = c(1, 1, 1, 1) + 0.1) 
text(0,1,paste(“MRN:”,MRN), adj=c(0,1), font=2)
text(0.4,1,paste(“Sex:”,SEX), adj=c(0,1))
text(0.6,1,paste(“Risk factor:”,RISKFAC1), adj=c(0.5,1))
text(0,0.85,paste(“DOB:”,dob), adj=c(0,1))
text(0.4,0.85,paste(“1st ARV:”,fad), adj=c(0,1))
text(0,0.7,paste(“1st clinic visit:”,fvd), adj=c(0,1))
text(0.4,0.7,paste(“Last clinic visit:”,lvd), adj=c(0,1))

# Drug table
arv <-read.xport('arv')
ond <- as.date(ON)
offd <- as.date(OFF)
par(mar = c(1, 1, 1, 1) + 0.1) 
text(0,1,paste(“Start date”), adj=c(0,1), font=2)
text(0.3,1,paste(“Stop date”), adj=c(0.5,1) , font=2)
text(0.6,1,paste(“ARVs”), adj=c(0.5,1) , font=2)
text(0,0.85,paste(ond, collapse='\n\n'), adj=c(0,1))
text(0.3, 0.85,paste(offd, collapse='\n\n'), adj=c(0.5,1))
text(0.6, 0.85,paste(ARVS, collapse='\n\n'), adj=c(0.5,1))

When I use it in the loop (adding [pt] to the variable names, of course), I 
don’t get all the data—it skips lines in some tables or something. I don’t 
understand the mechanism behind ‘for’, but it does not seem to just substitute 
in a value like in a SAS macro variable. So what I tried was making one big 
table (in SAS and replacing duplicated data with missing values—i.e. ‘if not 
first.(variable) then (newvariable)=’.’, etc) so when there was more genotype 
entries than drug entries, for example, “NA”s filled the corresponding drug 
data cells. This results, however, in a lot of  “NA”s being printed in the 
table sections, since there are many lines of data for the graph data. 
Ccreating this big table was cumbersome, too.

Is there a way to create a loop when several data sources are being used? (Am 
I making any sense?) Or I thought I could put out a set df for each patient 
(e.g. arv1:arv(n); bl1:bl(n); etc) and use something like I &#61663; (1:193) 
and paste I into the import and attach commands. I don’t want to give up 
because I am really happy with the results when done one patient at a time so 
your continued help would so appreciated.

Thanks for your time!


>===== Original Message From Thomas Lumley <tlumley at u.washington.edu> =====
>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 
>> successful.
>> Here's what I want to do:
>> -Create and save the following overlayed scatterplots for each subject (1 
>> n).
>> -The data frame is in the form of one line per visit per subject (so more 
>> 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 
>> 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 
>  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