Skip to content

Plots by subject

1 message · Tanya Murphy

#
Thomas,
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 
with).


So the whole function for one patient is:
{
# Overlay plot
lab <-read.xport('lab')
attach(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=??)
par(new=T)
plot(gtd, Y, ylim=c(0,800), type='p', pch=8, axes=F, ylab='', xlab='', 
cex=1.4)
par(new=T)
plot(vld, VL, axes=F, ylim=c(0,6), lty=5, ylab='', type='c', xlab='')
axis(4)
par(new=T)
plot(vld, VL0, axes=F, ylim=c(0,6), pch=16, lty=2, ylab='', type='p', xlab='', 
cex=1.4)
axis(4)
par(new=T)
plot(vld, VL1, axes=F, ylim=c(0,6), pch=1, lty=2, ylab='', type='p', xlab='', 
cex=1.4)
axis(4)
mtext("Viral load (log10) [--o--]",line=3, 4, outer=F, cex=0.7)
detach(lab)

# Genotype table
gt <-read.xport('gt')
attach(gt)
spd <- as.date(SPECDATE)
par(mar = c(0, 1, 1, 1) + 0.1) 
plot.new()
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))
detach(gt)

# Heading table
bl <-read.xport('bl')
attach(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) 
plot.new()
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))
detach(bl)

# Drug table
arv <-read.xport('arv')
attach(arv)
ond <- as.date(ON)
offd <- as.date(OFF)
par(mar = c(1, 1, 1, 1) + 0.1) 
plot.new()
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))
detach(arv)
}

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!

Tanya
wasn't
to
than
arguments
ID=",patient))
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._