adayim / forestploter

Draw forestplot in R
Other
79 stars 10 forks source link

Aligning xlab #43

Closed nsandau closed 1 year ago

nsandau commented 1 year ago

Hi,

Thank you so much for an excellent package.

I have a question regarding the alignment of the xlab. I have managed to create the following forestplot, but I would like the xlab to be center aligned. Is that possible at the moment? - Also, is it possible to adjust the font size just for the xlab?

billede

The data and code used to produce the plot is the following:

tribble(~term, ~estimate, ~std.error, ~statistic, ~p.value, ~n_mv, ~nevents_mv, ~hz, ~hz_str, ~lower, ~upper, ~lower_str, ~upper_str, ~estimate_str, ~ " ", ~  "  " , ~mv_hz, ~mv_lower, ~mv_upper, ~mv_estimate_str,
'leukocyt_log_scaled', 0.06, 0.02, 2.46, 0.01, 102438, 1627, 1.06, '1.06', 1.01, 1.11, '1.01', '1.11', '1.06 (1.01 - 1.11)', '                                        ', '                                        ', 1.03, 0.98, 1.08, '1.03 (0.98 - 1.08)',
'bas_diff_log_scaled', 0.03, 0.02, 2.12, 0.03, 102434, 1627, 1.04, '1.04', 1, 1.07, '1.00', '1.07', '1.04 (1.00 - 1.07)', '                                        ', '                                        ', 1.03, 1, 1.07, '1.03 (1.00 - 1.07)',
'eos_diff_log_scaled', 0.01, 0.02, 0.58, 0.56, 102414, 1626, 1.01, '1.01', 0.97, 1.06, '0.97', '1.06', '1.01 (0.97 - 1.06)', '                                        ', '                                        ', 1, 0.96, 1.05, '1.00 (0.96 - 1.05)',
'lym_diff_log_scaled', 0.03, 0.02, 1.22, 0.22, 102417, 1626, 1.03, '1.03', 0.98, 1.07, '0.98', '1.07', '1.03 (0.98 - 1.07)', '                                        ', '                                        ', 1.01, 0.96, 1.05, '1.01 (0.96 - 1.05)',
'mon_diff_log_scaled', 0.06, 0.02, 2.64, 0.01, 102415, 1626, 1.06, '1.06', 1.02, 1.11, '1.02', '1.11', '1.06 (1.02 - 1.11)', '                                        ', '                                        ', 1.05, 1, 1.1, '1.05 (1.00 - 1.10)',
'neu_diff_log_scaled', 0.06, 0.03, 2.35, 0.02, 102026, 1614, 1.06, '1.06', 1.01, 1.12, '1.01', '1.12', '1.06 (1.01 - 1.12)', '                                        ', '                                        ', 1.03, 0.98, 1.09, '1.03 (0.98 - 1.09)')
tm <- forest_theme(core = list(
    bg_params = list(fill = c("#ffffff"))
))

ticks <- seq(from = 0.95, to = 1.15, by = 0.1)  %>%  c(., 1)  %>%  sort()

fp <- fp_data %>%
    select(term, n_mv, nevents_mv, estimate_str, " ", mv_estimate_str, "  ") %>%
    rename(
        "Cell type" = term,
        "Hazard Ratio \n(95% CI)" = estimate_str,
        "Hazard Ratio \n(95% CI) " = mv_estimate_str,
        "Individuals, \nNo." = n_mv,
        "Events, \nNo." = nevents_mv
    ) %>%
    forest(
        est = list(fp_data$hz, fp_data$mv_hz),
        lower = list(fp_data$lower, fp_data$mv_lower),
        upper = list(fp_data$upper, fp_data$mv_upper),
        sizes = 0.75,
        ci_column = c(5, 7),
        ref_line = 1,
        xlim = c(0.95, 1.15),
        x_trans = "log",
        ticks_at = ticks,
        theme = tm,
        xlab = "Hazard ratio (95% CI)"
    ) %>%
    insert_text(
        text = " Model 1\n(age- and sex-adjusted)",
        part = "header",
        col = 4,
        gp = grid::gpar(fontface = "bold"),
        just = "left"
    ) %>%
    add_text(
        text = " Model 2\n(multivariable adjusted)",
        part = "header",
        row = 1,
        col = 6,
        gp = grid::gpar(fontface = "bold"), just = "left"
    ) %>%
    add_border(
        part = "header",
        row = 2,
        col = 1:4,
        gp = grid::gpar(lwd = .5)
    )

Thank you!

adayim commented 1 year ago

Sorry, it doesn't support adjusting the xlab or fontsize. But I will implement this, thank you for reporting this.

nsandau commented 1 year ago

Thank you for your response, and for planning to implement it.

And thank you again for developing this package. Really enjoy using it!

adayim commented 1 year ago

This has been added now. You can change the alignment in theme with xlab_adjust and xlab_cex for alignment and font size. Sorry for taking so long to add this.

nsandau commented 1 year ago

Thank you @adayim !