0

I would like to plot hazard ratios of exposure variable only, from several models in one plot that would also display p-values and CI. I can't use the hazard ratios plot e.g. from rms library as it would plot all variables HR based on 1 model. I found the code in this question Hazard ratios plot to do almost exactly what I would like to achieve but as it plots hazard ratios for some variables from one model, the hazard ratios are in the same colour.

I would like to create a very similar plot, but with hazard ratios and CI for different models in different colours. I tried to modify the code in link, but it did not work as I expected it to.

I modified dataframe and plot for my data , based on the code from the link.

mydf1 <- data.frame(
    SubgroupH=c('Female',NA,'Male',NA),
    Subgroup=c(NA,"Age under 60",NA,'Age under 60'),
    NoOfPatients=c(NA,1000,NA,800),
    HazardRatio=c(NA,0.90,NA,1.20),
    HazardLower=c(NA,0.77,NA,1.01),
    HazardUpper=c(NA,1.07,NA,1.40),
    Pvalue=c(NA,0.10,NA,0.03),
    stringsAsFactors=FALSE
)


rowseq <- seq(nrow(mydf1),1)
par(mai=c(1,0,0,0))

 plot(mydf1$HazardRatio, rowseq, pch=15,
    xlim=c(-10,12), ylim=c(0,7),
    xlab='', ylab='', yaxt='n', xaxt='n',
    bty='n')
axis(1, seq(-2,2,by=.4), cex.axis=.5)

segments(1,-1,1,4.25, lty=3) #4.25
segments(mydf1$HazardLower, rowseq, mydf1$HazardUpper, rowseq)

mtext('Decreased risk',1, line=2.5, at=0, cex=.5, font=2)
mtext('Increased risk',1.5, line=2.5, at=2, cex=.5, font=2)

text(-8,4.5, "Model", cex=.75, font=2, pos=4)
t1h <- ifelse(!is.na(mydf1$SubgroupH), mydf1$SubgroupH, '')
text(-8,rowseq, t1h, cex=.75, pos=4, font=3)
t1 <- ifelse(!is.na(mydf1$Subgroup), mydf1$Subgroup, '')
text(-7.5,rowseq, t1, cex=.75, pos=4)

text(-5,4.5, "No. of\nPatients", cex=.75, font=2, pos=4)
t2 <- ifelse(!is.na(mydf1$NoOfPatients), format(mydf1$NoOfPatients,big.mark=","), '')
text(-3, rowseq, t2, cex=.75, pos=2)

text(-1,4.5, "Hazard Ratio (95%)", cex=.75, font=2, pos=4)
t3 <- ifelse(!is.na(mydf1$HazardRatio), with(mydf1, paste(HazardRatio,' (',HazardLower,'-',HazardUpper,')',sep='')), '')
text(3,rowseq, t3, cex=.75, pos=4)

text(7.5,4.5, "P Value", cex=.75, font=2, pos=4)
t4 <- ifelse(!is.na(mydf1$Pvalue), mydf1$Pvalue, '')
text(7.5,rowseq, t4, cex=.75, pos=4)

I would be vey grateful for help.

Milo
  • 25
  • 2
  • Is passing a "col" argument to plot and segment not possible? It seems that you could create a column of desired colors in your data frame and then use that in plot in segment. – Calvin Jun 15 '21 at 01:28

0 Answers0