[R] How to add lines to lattice plot produced by rms::bplot

David Winsemius dwinsemius at comcast.net
Sat Aug 14 02:33:16 CEST 2010


I have a plot produced by function bplot (package = rms) that is  
really a lattice plot (class="trellis"). It is similar to this plot  
produced by a very minor modification of the first example on the  
bplot help page:

requiere(rms)
n <- 1000    # define sample size
set.seed(17) # so can reproduce the results
age            <- rnorm(n, 50, 10)
blood.pressure <- rnorm(n, 120, 15)
cholesterol    <- rnorm(n, 200, 25)
sex            <- factor(sample(c('female','male'), n,TRUE))
label(age)            <- 'Age'      # label is in Hmisc
label(cholesterol)    <- 'Total Cholesterol'
label(blood.pressure) <- 'Systolic Blood Pressure'
label(sex)            <- 'Sex'
units(cholesterol)    <- 'mg/dl'   # uses units.default in Hmisc
units(blood.pressure) <- 'mmHg'

# Specify population model for log odds that Y=1
L <- .4*(sex=='male') + .045*(age-50) +
   (log(cholesterol - 10)-5.2)*(-2*(sex=='female') + 2*(sex=='male'))
# Simulate binary y to have Prob(y=1) = 1/[1+exp(-L)]
y <- ifelse(runif(n) < plogis(L), 1, 0)

ddist <- datadist(age, blood.pressure, cholesterol, sex)
options(datadist='ddist')

fit <- lrm(y ~ blood.pressure + sex * (age + rcs(cholesterol,4)),
                x=TRUE, y=TRUE)
p <- Predict(fit, age, cholesterol, sex='male', np=50) # vary sex last
bp.plot <- bplot(p, lfun=contourplot)
bp.plot

I have tried a variety of efforts at using update (which I assume is a  
lattice function although I can find no help page for it. It does  
appear in some of the lattice hep pages and my understanding is that  
it pushes objects onto the list structure of a plot object. I've also  
tried adding to it with llines()
#-------------
Oh, never mind. I recovered a memory that I had seen a solution on  
rhelp and had saved it. Turns out it was from Peter Ehlers, to whom I  
offer thanks. I was trying to add a step function: ht and weight from  
a dataframe, "bld":

trellis.focus("panel", 1, 1)
    panel.lines(x=bld$inches, y=bld$BMI28, type='s')
  trellis.unfocus()

Success!

Now... how do I control the color levels in levelplot or contourplot???


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list