gforge / forestplot

Advanced forest plots in R using grid graphics
44 stars 15 forks source link

Horizontal forestplot bars with forestplot package #66

Closed Antoine-KDA closed 3 months ago

Antoine-KDA commented 3 months ago

I was trying to plot a forest plot for different countries using the example below:

toydt <- data.frame(
  Types = c("All SPM types", "All SPM types (adjusted)", "Solid cancer", "Skin cancers", "MDS/AML",
            "All SPM types", "All SPM types (adjusted)", "Solid cancer", "Skin cancers", "MDS/AML",
            "Hematologic", "Hematologic", "All SPM types", "All SPM types (adjusted)", "Solid cancer",
            "Skin cancers", "MDS/AML", "Hematologic"),
  HR = c(1.42, 1.44, 1.05, 2.25, 2.21, 2.4, 2.7, 1.9, 3.6, 3.6, 2.1, 2.21, 1.847, 1.971, 1.415,
         2.847, 2.361, 2.186),
  Lower = c(1.17, 1.18, 0.8, 1.64, 1.15, 2, 2.2, 1.5, 2.6, 0.7, 0.6, 1.15, 1.105, 1.064, 0.791,
            1.796, 1.285, 1.224),
  Upper = c(1.72, 1.75, 1.38, 3.1, 4.25, 2.9, 3.3, 2.5, 4.9, 19.1, 7.4, 4.25, 3.09, 3.65, 2.53,
            4.51, 4.34, 3.90),
  Country = c("Denmark", "Denmark", "Denmark", "Denmark", "Denmark", "Sweden", "Sweden", "Sweden",
              "Sweden", "Sweden", "Sweden", "Denmark", "Both countries", "Both countries", "Both countries",
              "Both countries", "Both countries", "Both countries"),
  HR_CI_Tau_TauSq_Isq = c("1.4 (1.2, 1.7)", "1.4 (1.2, 1.8)", "1.0 (0.8, 1.4)", "2.2 (1.6, 3.1)",
                          "2.2 (1.1, 4.2)", "2.4 (2.0, 2.9)", "2.7 (2.2, 3.3)", "1.9 (1.5, 2.5)",
                          "3.6 (2.6, 4.9)", "3.6 (0.7, 19.1)", "2.1 (0.6, 7.4)", "2.2 (1.1, 4.2)",
                          "1.8 (1.1, 3.1); 0.4; 0.1; 93.2", "2.0 (1.1, 3.6); 0.4; 0.2; 94.7",
                          "1.4 (0.8, 2.5); 0.4; 0.2; 89.7", "2.8 (1.8, 4.5); 0.3; 0.1; 76.2",
                          "2.4 (1.3, 4.3); 0.0; 0.0; 0.0", "2.2 (1.2, 3.9); 0.0; 0.0; 0.0")
)

toydt |> 
   group_by(Country) |> 
   forestplot(mean = HR,
            lower = Lower,
            upper = Upper,
            labeltext = c("Types"),
            clip = c(0, 12),
            zero = 1,
            boxsize = 0.15,
            lty.ci = c(3,2,1),
            col = fpColors(box = "royalblue", lines = "darkblue", zero = "black"),
            xlab = "Hazard Ratio (95% CI)" , #align = "c"
            graph.pos = 2,
            xlog = FALSE,  # Linear scale
            xticks = c(1, seq(0, 12, by = 2)),  # X-axis range from -2 to 22
            ci.vertices = TRUE, 
            lwd.xaxis = 2, 
            lwd.zero = 2, 
            lwd.ci = 3, 
            txt_gp = fpTxtGp(xlab = gpar(fontsize = 22), 
                             ticks = gpar(fontsize = 22))) |>
    fp_set_zebra_style("#FFFFFF", "#EFEFEF")|>

   fp_set_style(box = c("#FF6A6A", "darkred", "royalblue") |> 
                  lapply(function(x) gpar(fill = x, col = x)), 

                line = c("#FF6A6A", "darkred", "darkblue") |> 
                  lapply(function(x) gpar(lwd = 3, col = x))) |>
    fp_decorate_graph(grid = structure(c(0, 2, 4, 6, 8, 10, 12)))

The code worked well and I got the nice figure attached.

However I wanted to add on the graphic one right column to display the HR along with the 95% CI but the description of the labeltxt argument is quite unclear about how to deal with this. Is there a way to introduce 1 or 2 columns on the right displaying the corresponding HR and 95%CI for each study?

sessionInfo() R version 4.3.3 (2024-02-29 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.utf8 LC_CTYPE=English_United Kingdom.utf8
[3] LC_MONETARY=English_United Kingdom.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.utf8

time zone: Europe/Berlin tzcode source: internal

attached base packages: [1] grid stats graphics grDevices datasets utils methods base

other attached packages: [1] tidyr_1.3.1 forestplot_3.1.3 abind_1.4-5 checkmate_2.3.1 magrittr_2.0.3 meta_7.0-0
[7] metadat_1.2-0 dplyr_1.1.4 All_cause_mortality_meta_HRs

gforge commented 3 months ago

There should be. Try to ask this question on StackOverflow and look into the vignettes where there should be an example that shows hot to do this