David Winsemius
2010-Aug-14 00:33 UTC
[R] How to add lines to lattice plot produced by rms::bplot
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
Duncan Mackay
2010-Aug-14 03:25 UTC
[R] How to add lines to lattice plot produced by rms::bplot
Hi David I do not know if you have done something like this. 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 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. >