2

I draw a calibration plot with the R code from:http://rpubs.com/IL2/519772

require(rms)
library(Hmisc)
library(grid)
library(lattice)
library(Formula) 
library(ggplot2) 
library(survival)
library(survival)
data(lung)

lung$sex <- factor(lung$sex,levels = c(1,2),labels = c("male", "female"))
dd=datadist(lung)
options(datadist="dd") 
fit1<- lrm(status~ age + sex+ ph.karno,x=T,y=T, data = lung) 
cal1 <- calibrate(fit1,X=T,Y=T, method='boot',m=76,B=228)

plot(cal1,lwd=2,lty=1,
 cex.lab=1.2, cex.axis=1, cex.main=1.2, cex.sub=0.6,
 xlim = c(0,1),ylim= c(0,1),
 xlab="Nomogram-Predicted Probability of death risk",
 ylab="Actual death (proportion)",
 col=c("#00468BFF","#ED0000FF","#42B540FF")
 )

 lines(cal1[,c(1:3)], 
  type ="o", 
  lwd = 1, 
  pch = 16, 
  col=c("#00468BFF"))

abline(0,1,lty = 3, 
   lwd = 2, 
   col = c("#224444")
   ) 

However, my plot cannot show all the legend of "apparent, Bias-corrected, Ideal".

enter image description here How to edit the plot like the following? enter image description here

  • It's easier to help you if you include a simple [reproducible example] (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 13 '20 at 05:19
  • 1
    I don't think it's always reasonable to expect readers here on SO to read through the remote article and run all of its code trying to get to the point where this question becomes relevant. Further, it is generally better to keep questions here on SO as self-contained as possible; while I would not expect rpubs links to go stale, if it does ... this question as-is becomes useless. – r2evans Jan 13 '20 at 05:21
  • Thank you very much for your comments about the formatting, I revised the details. – Jenny ZHANG Jan 13 '20 at 05:41
  • link is dead..... – jtlz2 Jul 12 '22 at 13:41

1 Answers1

4

You could use legend=FALSE option and create a custom legend and define exact positions.

plot(cal1, lwd=2, lty=1, 
     cex.lab=1.2, cex.axis=1, cex.main=1.2, cex.sub=0.6, 
     xlim=c(0, 1), ylim= c(0, 1), 
     xlab="Nomogram-Predicted Probability of death risk", 
     ylab="Actual death (proportion)", 
     col=c("#00468BFF", "#ED0000FF", "#42B540FF"),
     legend=FALSE)

lines(cal1[, c(1:3)], type ="o", lwd=1, pch=16, col=c("#00468BFF"))

abline(0, 1, lty=3, lwd=2,  col=c("#224444")) 

legend(x=.6, y=.4, legend=c("Apparent", "Bias-corrected", "Ideal"), 
       lty=c(3, 1, 2), bty="n")

enter image description here

As you just found out on your own, to make the legend even narrower we can use option y.intersp inside legend, e.g. legend(... y.intersp=.8).

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thank you so much for your useful suggestions. However, the spaces among the three legends are too wide in my version, how can I change that? thank you again. – Jenny ZHANG Jan 13 '20 at 06:54
  • 1
    Thank you so much, I solved the problem of legend space: legend(x=.5, y=.4, legend=c("Apparent", "Bias-corrected", "Ideal"), lty=c(3, 1, 2), bty="n",y.intersp=0.3) – Jenny ZHANG Jan 13 '20 at 06:58
  • Nice! Consider, that it may look different when you [export the plot](https://stackoverflow.com/a/7144203/6574038). – jay.sf Jan 13 '20 at 07:07