adayim / forestploter

Draw forestplot in R
Other
79 stars 10 forks source link

Adding superscript symbols into body of table #60

Closed r-forrest closed 3 weeks ago

r-forrest commented 8 months ago

It would be useful to add a superscript symbol i.e., asterisk, dagger or double dagger into cells of the table so that it is possible to link this to a corresponding footnote. E.g., for a meta-analysis, where the name of studies are listed.

adayim commented 7 months ago

Hi,

I don't have any data to try but you can use the following method:

# Check out the `plotmath` function for math expression.
dt <- data.frame(
  Study = c("Study ~1^a", "Study ~2^b", "NO[2]"),
  low = c(0.2, -0.03, 1.11),
  est = c(0.71, 0.35, 1.79),
  hi = c(1.22, 0.74, 2.47)
)

# dt$Study <- parse(text = dt$Study)

dt$SMD <- sprintf("%.2f (%.2f, %.2f)", dt$est, dt$low, dt$hi)
dt$` ` <- paste(rep(" ", 20), collapse = " ")

fig_dt <- dt[,c(1,5:6)]

# Get a matrix of which row and columns to parse
parse_mat <- matrix(FALSE, 
                    nrow = nrow(fig_dt),
                    ncol = ncol(fig_dt))

# Here we want to parse the first column only, you can amend this to whatever you want.
parse_mat[,1] <- TRUE  

tm <- forest_theme(colhead=list(fg_params = list(parse=TRUE)), # Remove this fi you don't want parse the column head.
                   core=list(fg_params = list(parse=parse_mat)))

forest(fig_dt,
       est = dt$est,
       lower = dt$low,
       upper = dt$hi,
       ci_column = 3,
       theme = tm)

If you want to add or insert text that needs to be parsed, check out the add_text and insert_text help. Check out plotmath to find out more on the math expression.

You can simply set tm <- forest_theme(core=list(fg_params = list(parse=TRUE))) to parse all the text inside the data.frame. But the parse will remove the extra blank space in the data and you will end up having narrow column to draw the confidence interval.