Forest plot and table overlap

67 views Asked by At

I am trying to plot a forest plot with Hazards ratios from a Coxph model. I have resorted to making it manually because the model contains a cluster=id function and a stratification which ggforest can't seem to handle. Here is some reproducible data and my current solution:

mydf<- data.frame(
  SubgroupH=c('var1',"Age",NA,NA,NA,'var2',NA,NA,NA,
              "var3", "Early", "Middle", "Late"),
  Subgroup=c(NA,NA,'<50','50-59',">60",NA,'sub1','sub2',"sub3",NA,NA,NA,NA),
  HazardRatio=c(0.73,NA,NA,1.52,4.8,NA,NA,0.76,1.27,1.78,
                0.10,2.86,48.4),
  HazardLower=c(0.22,NA,NA,0.51,1.89,NA,NA,0.19,0.49,0.96,
                0.01,1.22,11.1),
  HazardUpper=c(1.81,NA,NA,3.45,11.6,NA,NA,2.63,4.5,3.32,
                0.79,7.48,232),
  Pvalue=c(0.4,NA,NA,0.6,0.001,NA,NA,0.6,0.8,0.069,
           0.029,0.046,0.001),
  stringsAsFactors=FALSE
)

#Plotting
rowseq <- seq(nrow(mydf),1)
par(mar=c(3,9,1,10))
plot(mydf$HazardRatio, rowseq, pch=15,
     xlim=c(-60,250), ylim=c(0,15),
     xlab='', ylab='', yaxt='n', xaxt='n',
     bty='n')
axis(1, seq(-60,250,by=20), cex.axis=.5)

segments(1,-1,1,14, lty=3)
segments(mydf$HazardLower, rowseq, mydf$HazardUpper, rowseq)

text(-60,14, "Subgroup", cex=.50, font=2, pos=4)
t1h <- ifelse(!is.na(mydf$SubgroupH), mydf$SubgroupH, '')
text(-60,rowseq, t1h, cex=.5, pos=4, font=3)
t1 <- ifelse(!is.na(mydf$Subgroup), mydf$Subgroup, '')
text(-50,rowseq, t1, cex=.5, pos=4)

text(-15,11, "Ref", cex=.5, font=2, pos=4)
text(-15,7, "Ref", cex=.5, font=2, pos=4)

text(0,15, "Hazard Ratio (95%)", cex=.75, font=2, pos=4)
t3 <- ifelse(!is.na(mydf$HazardRatio), with(mydf, paste(HazardRatio,' (',HazardLower,'-',HazardUpper,')',sep='')), '')
text(100,rowseq, t3, cex=.5, pos=4)

text(200,14, "P Value", cex=.5, font=2, pos=4)
t4 <- ifelse(!is.na(mydf$Pvalue), mydf$Pvalue, '')
text(200,rowseq, t4, cex=.5, pos=4)

#dev.off()

enter image description here

My problem is that the last parameter "Late" has an upper limit of 232 so that crosses out the table and makes the CIs of the other variables disappear to fit it in the graph.

Can anyone come up with a solution to make this more clean?

0

There are 0 answers