0

I'm new to R and making a forestplot for a meta-analysis and need to add a superscript after the Author/Year on the left side of the graph. I have tried various methods to get the superscript added but keep getting error. e.g. when I try to use the expression function it states that forestplot cannot accept any expression values. Ideally I would like to use column names for both the author/year as well as the reference number to be superscripted, but I would be happy to just type them out at this point if it would work.

Basically in my dataframe "m" I have a column for the authors and years "fulltitle" as char (I wrote them out here in the hopes that expression would work), another column "refnum" that contains the reference number to be superscripted, and then the numeric data for the actual graph which is the mean and upper/lower CIs. If I use the studies without list(expression()) then it will work fine but I won't have any superscript after the title/year.

#Non-functioning code that gives error in bind_rows or !is_expression

#I'm using lattice as I am making another table with 112 studies that would not display otherwise 
    library(lattice)
    trellis.device(device="windows", height = 4, width = 10, color=TRUE)
    base_data <- tibble(mean  = (m$overall_survival_mean_of_months),
                    lower = (m$CILower),
                    upper = (m$CIUpper),
                    study = list(expression("N 2000"^2), 
                                 expression("S 1992"^1),
                                 expression("K 2012"^1),
                                 expression("S 2013"^1),
                                 expression("A 2012"^1),
                                 expression("S 2005"^1),
                                 expression("M 2001"^1),
                                 expression("B 1993"^1),
                                 expression("B 2017"^1),
                                 expression("H 2013"^1),
                                 expression("L 2017"^1),
                                 expression("S 2018"^1)),
                    sample_size = as.character(m$number_of_patients),
                    OS = as.character(m$overall_survival_mean_of_months))
    summary <- tibble(mean  = 6, 
                  lower = 2,
                  upper = 10,
                  study = expression("Studies"),
                  sample_size = "1,000 patients (12 studies)",
                  OS = "6.0 ± 4.0 months",
                  summary = TRUE)
    header <- tibble(study = c(expression("", "Study")),
                 sample_size = c("", "Number of Patients"),
                 OS = c("", "Overall Survival"),
                 summary = TRUE)    empty_row <- tibble(mean = NA_real_)
    cochrane_output_df <- bind_rows(header,
                                base_data,
                                empty_row,
                                summary)
    cochrane_output_df %>% 
    forestplot(labeltext = c(study, sample_size, OS), 
             is.summary = summary,
             clip = c(0,40), 
             exp = FALSE,
             colgap = unit(4,"mm"),
             xlog = FALSE, 
             txt_gp = fpTxtGp(cex = 0.5),
             col = fpColors(box = "royalblue",
                            line = "darkblue",
                            summary = "royalblue"))
    #Error in if (!is.expression(row_column_text) && !is.call(row_column_text) &&  : missing value where TRUE/FALSE needed

Working code (without superscripts)

base_data <- tibble(mean  = (m$overall_survival_mean_of_months),
                    lower = (m$CILower),
                    upper = (m$CIUpper),
                    study = c(as.character(("N 2000"), 
                                 ("S 1992"),
                                 ("K 2012"),
                                 ("S 2013"),
                                 ("A 2012"),
                                 ("S 2005"),
                                 ("M 2001"),
                                 ("B 1993"),
                                 ("B 2017"),
                                 ("H 2013"),
                                 ("L 2017"),
                                 ("S 2018"))),
                    sample_size = as.character(m$number_of_patients),
                    OS = as.character(m$overall_survival_mean_of_months))
summary <- tibble(mean  = 6, 
                  lower = 2,
                  upper = 10,
                  study = "Studies",
                  sample_size = "1,000 patients (12 studies)",
                  OS = "6.0 ± 4.0 months",
                  summary = TRUE)

header <- tibble(study = c("", "Study"),
                 sample_size = c("", "Number of Patients"),
                 OS = c("", "Overall Survival"),
                 summary = TRUE)

empty_row <- tibble(mean = NA_real_)

cochrane_output_df <- bind_rows(header,
                                base_data,
                                empty_row,
                                summary)

cochrane_output_df %>% 
  forestplot(labeltext = c(study, sample_size, OS), 
             is.summary = summary,
             clip = c(0,40), 
             exp = FALSE,
             colgap = unit(4,"mm"),
             xlog = FALSE, 
             txt_gp = fpTxtGp(cex = 0.5),
             col = fpColors(box = "royalblue",
                            line = "darkblue",
                            summary = "royalblue"))`

Here's the data (output from dput):

structure(list(number_of_patients = c(36L, 20L, 56L, 51L, 39L, 
125L, 5L, 528L, 32L, 127L, 177L, 129L), overall_survival_mean_of_months = c(7.3, 
10.8, 10, 10.8, 5, 14.5, 9.5, 2.9, 14.6, 6.7, 9.3, 4), fulltitle = c("A 2012", "B 2017", "B 1993", "H 2013", "K 2012", 
"L 2017", "M 2001", "N 2000", "S 2018", 
"S 2013", "S 2005", "S 1992"), CILower = c(5.32596600574852, 
8.15155547937496, 8.41725166935702, 9.14147970759323, 3.10340948571217, 
13.44062219175, 4.20311095874991, 2.38454718425576, 12.506220764558, 
5.64899685123535, 8.40973511344699, 2.95717598838621), CIUpper = c(9.27403399425148, 
13.448444520625, 11.582748330643, 12.4585202924068, 6.89659051428783, 
15.55937780825, 14.7968890412501, 3.41545281574424, 16.693779235442, 
7.75100314876465, 10.190264886553, 5.04282401161379), refnum = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)), class = "data.frame", row.names = c(NA, 
-12L))
Mike
  • 1
  • 1
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. How exactly are you calling the forest plot function? – MrFlick Aug 04 '22 at 18:41
  • Need to make a reproducible example – GK89 Aug 05 '22 at 00:22
  • Sorry I didn't see these earlier. Please let me know if more info is needed and I appreciate you both taking the time to have a look! – Mike Aug 05 '22 at 19:34
  • Hey, I found that it was easier to use Illustrator to alter the plot afterwards than to try and make it work in r so while I did not find a solution through r I did make it work! – Mike Aug 06 '22 at 20:55

0 Answers0