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

Frank Harrell f.harrell at vanderbilt.edu
Sat Aug 14 17:08:29 CEST 2010



Frank E Harrell Jr   Professor and Chairman        School of Medicine
                      Department of Biostatistics   Vanderbilt University

On Sat, 14 Aug 2010, David Winsemius wrote:

>
> On Aug 14, 2010, at 9:59 AM, Frank Harrell wrote:
>
>>
>> Once you guys figure all this out, I'm glad to modify bplot to pass
>> more arguments lattice if needed.
>
> As always, Frank, I appreciate your support. In this case I think it's
> not needed. What seems to be needed is simply the correct use of the
> "at" argument. (I thought I had tried this before but apparently
> mucked it up somehow.)  This gives the desired color levels separation
> and labeling of the default levelplot version of bplot output:
>
>   bldLT40 <- bplot(mdl.pred , perim=boundaries, at=c(-0.233, seq(.50,
> 2.25, by=0.25), seq(2.5, 5.0, by=0.5), 6:10) )
>  bldLT40
>
> And this produces the expected output with its contouplot version:
>
> bldLT40c <- bplot(mdl.pred , perim=boundaries, lfun=contourplot,
> at=c(-0.233, seq(.50, 2.25, by=0.25), seq(2.5, 5.0, by=0.5), 6:10) )
>  bldLT40c
>
> My only quibble with that last one was that the labels had three
> digits to the right of the decimal pt but that happily went away when
> I changed the low end to 0.25. All is good here.
>
> -- 
> David.

Excellent.  Thanks David.  If you think of anything that would be a 
good addition to the examples or other parts of the help file, shoot 
me a few lines and I'll paste them in.

Frank

>>
>> Frank E Harrell Jr   Professor and Chairman        School of Medicine
>>                     Department of Biostatistics   Vanderbilt
>> University
>>
>> On Fri, 13 Aug 2010, David Winsemius wrote:
>>
>>>
>>> On Aug 13, 2010, at 11:25 PM, Duncan Mackay wrote:
>>>
>>>> Hi David
>>>>
>>>> I do not know if you have done something like this.
>>>
>>> I had tried a few efforts like that, starting with an examination of
>>> str(bp.plot) as you demonstrate.
>>>
>>>> I tried str(bp.plot) which gave the section about the regions (for
>>>> colours) as:
>>>> $ panel.args.common:List of 8
>>>> ..$ x      : num [1:2500] 27 28 29 29.9 30.9 ...
>>>> ..$ y      : num [1:2500] 141 141 141 141 141 ...
>>>> ..$ z      : num [1:2500] -1.43 -1.41 -1.39 -1.36 -1.34 ...
>>>> ..$ at     : num [1:10] -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
>>>> ..$ region : logi FALSE
>>>> ..$ zlab   :List of 3
>>>> .. ..$ label: chr "log odds"
>>>> .. ..$ rot  : num 90
>>>> .. ..$ cex  : num 1
>>>> ..$ labels : logi TRUE
>>>> ..$ contour: logi TRUE
>>>
>>> I tried (with a bplot object named "bldLT40"):
>>>
>>> bldLT40$legend$right$args$key$at <- c(-0.233, seq(.50, 2.25,
>>> by=0.25),
>>> seq(2.5, 5.0, by=0.5), 6:10)
>>>
>>> ... and then tried
>>>
>>> bldLT40$panel.args$at <- c(-0.233, seq(.50, 2.25, by=0.25), seq(2.5,
>>> 5.0, by=0.5), 6:10)
>>>
>>> Neither of these efforts changed the boundaries beteen colors in the
>>> plot area. The first effort changed the legend scal,e but that just
>>> created a "misalignment" of the colors of plot area and the legend. I
>>> would be interested in either a strategy that lets one alter the
>>> color
>>> level changes of the z variable (which in bplot-created objects is
>>> "zhat", or lets one specify the values at which contour lines are
>>> drawn in contourplot.
>>>
>>> Thanks for your efforts.
>>>
>>> --
>>> David.
>>>
>>>
>>>>
>>>> I added the col.region and colours from a plot levelplot that I had
>>>> done to see what would occur to the trellis parameters. No colours
>>>> were produced when plotted.
>>>> bp.plot <- bplot(p, lfun=contourplot, color.key = TRUE, col.regions
>>>> =
>>>> c
>>>> ("#FFFFFF
>>>> ","#00FFFF","#A9E2FF","#8080FF","#0000FF","#FFD18F","#FF0000") )
>>>>
>>>> $ panel.args.common:List of 10
>>>> ..$ x          : num [1:2500] 27 28 29 29.9 30.9 ...
>>>> ..$ y          : num [1:2500] 141 141 141 141 141 ...
>>>> ..$ z          : num [1:2500] -1.43 -1.41 -1.39 -1.36 -1.34 ...
>>>> ..$ at         : num [1:10] -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
>>>> ..$ region     : logi FALSE
>>>> ..$ color.key  : logi TRUE
>>>> ..$ zlab       :List of 3
>>>> .. ..$ label: chr "log odds"
>>>> .. ..$ rot  : num 90
>>>> .. ..$ cex  : num 1
>>>> ..$ labels     : logi TRUE
>>>> ..$ contour    : logi TRUE
>>>> ..$ col.regions: chr [1:7] "#FFFFFF" "#00FFFF" "#A9E2FF"
>>>> "#8080FF" ...
>>>>
>>>> So it has been added to the panel.args.common, whether you can
>>>> access these are another matter.
>>>>
>>>> I then tried
>>>> bp.plot <- bplot(p, lfun=contourplot, par.settings = list(axis.text
>>>> = list(cex = 0.65)), color.key = TRUE, col.regions =
>>>> c
>>>> ("#FFFFFF
>>>> ","#00FFFF","#A9E2FF","#8080FF","#0000FF","#FFD18F","#FF0000") )
>>>>
>>>> which changed the size of the axis text so it may be the case of
>>>> having to add the col.regions etc to the appropriate list in
>>>> par.settings
>>>>
>>>> I'll leave you to amend and access the colours.
>>>> You may have to add values for the wireframe/levelplot arguments
>>>> like "at"  etc. and col.regions (I think that is the function) to
>>>> produce an appropriate colour range of your choice
>>>>
>>>> It is a while since I have delved into these sorts of plots.
>>>> Need some sustenance.
>>>>
>>>> Regards
>>>>
>>>> Duncan
>>>>
>>>> Duncan Mackay
>>>> Department of Agronomy and Soil Science
>>>> University of New England
>>>> ARMIDALE NSW 2351
>>>> Email home: mackay at northnet.com.au
>>>>
>>>>
>>>>
>>>> At 10:33 14/08/2010, you wrote:
>>>>
>>>>> 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:
>>>>>
>>>>> require(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
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>
> David Winsemius, MD
> West Hartford, CT
>



More information about the R-help mailing list