colon<-within(colon, {sex<-factor(sex, labels =c("female", "male"))differ<-factor(differ, labels =c("well", "moderate", "poor"))extent<-factor(extent, labels =c("submuc.", "muscle", "serosa", "contig."))})bigmodel<-coxph(Surv(time, status)~sex+rx+adhere+differ+extent+node4, data =colon)ggforest(bigmodel)#> Warning in .get_data(model, data = data): The `data` argument is not provided.#> Data will be extracted from model fit.
1.2 forestploter
Code
library(grid)library(forestploter)dt<-read.csv(system.file("extdata", "example_data.csv", package ="forestploter"))# Indent the subgroup if there is a number in the placebo columndt$Subgroup<-ifelse(is.na(dt$Placebo), dt$Subgroup,paste0(" ", dt$Subgroup))# NA to blankdt$Treatment<-ifelse(is.na(dt$Treatment), "", dt$Treatment)dt$Placebo<-ifelse(is.na(dt$Placebo), "", dt$Placebo)dt$se<-(log(dt$hi)-log(dt$est))/1.96# Add a blank column for the forest plot to display CI.# Adjust the column width with space. dt$` `<-paste(rep(" ", 20), collapse =" ")# Create confidence interval column to displaydt$`HR (95% CI)`<-ifelse(is.na(dt$se), "",sprintf("%.2f (%.2f to %.2f)",dt$est, dt$low, dt$hi))# Define themetm<-forest_theme(base_size =10, refline_col ="red", arrow_type ="closed", footnote_gp =gpar(col ="blue", cex =0.6))#> refline_col will be deprecated, use refline_gp instead.p<-forest(dt[,c(1:3, 20:21)], est =dt$est, lower =dt$low, upper =dt$hi, sizes =dt$se, ci_column =4, ref_line =1, arrow_lab =c("Placebo Better", "Treatment Better"), xlim =c(0, 4), ticks_at =c(0.5, 1, 2, 3), footnote ="This is the demo data. Please feel free to change\nanything you want.", theme =tm)# Print plotplot(p)
Code
# Edit text in row 3g<-edit_plot(p, row =3, gp =gpar(col ="red", fontface ="italic"))# Bold grouping textg<-edit_plot(g, row =c(2, 5, 8, 11, 15, 18), gp =gpar(fontface ="bold"))# Insert text at the topg<-insert_text(g, text ="Treatment group", col =2:3, part ="header", gp =gpar(fontface ="bold"))# Add underline at the bottom of the headerg<-add_border(g, part ="header", row =1, where ="top")g<-add_border(g, part ="header", row =2, where ="bottom")g<-add_border(g, part ="header", row =1, col =2:3, gp =gpar(lwd =2))# Edit the background of row 5g<-edit_plot(g, row =5, which ="background", gp =gpar(fill ="darkolivegreen1"))# Insert textg<-insert_text(g, text ="This is a long text. Age and gender summarised above.\nBMI is next", row =10, just ="left", gp =gpar(cex =0.6, col ="green", fontface ="italic"))g<-add_border(g, row =10, col =1:3, where ="top")plot(g)
1.3
Code
# Add a blank column for the second CI columndt$` `<-paste(rep(" ", 20), collapse =" ")# Set-up themetm<-forest_theme(base_size =10, refline_col ="red", footnote_gp =gpar(col ="blue"), legend_name ="GP", legend_value =c("Trt 1", "Trt 2"))#> refline_col will be deprecated, use refline_gp instead.p<-forest(dt[,c(1:2, 20, 3, 22)], est =list(dt$est_gp1,dt$est_gp2,dt$est_gp3,dt$est_gp4), lower =list(dt$low_gp1,dt$low_gp2,dt$low_gp3,dt$low_gp4), upper =list(dt$hi_gp1,dt$hi_gp2,dt$hi_gp3,dt$hi_gp4), ci_column =c(3, 5), ref_line =1, arrow_lab =c("Placebo Better", "Treatment Better"), nudge_y =0.2, x_trans ="log", theme =tm)plot(p)