Superscript numbers in a forestplot

I attempt to insert references in a forestplot and superscript the numbers. Here is part of my data and codes, the numbers[23], [22], [9], [21] in the first column is the inserted references:

hr1 ci.lb1 ci.ub1 hr2 ci.lb2 ci.ub2
Author HR(95%CI)
LM non-LM
Louis 2016[23] 0.64(0.37,1.1) 0.68(0.49,0.94) 0.64 0.37 1.1 0.68 0.49 0.94
Achim 2017[22] 0.82(0.59,1.14) 0.72(0.6,0.88) 0.82 0.59 1.14 0.72 0.6 0.88
Ramalingam et al 2019[9] 0.82(0.62,1.08) 0.72(0.62,0.85) 0.82 0.62 1.08 0.72 0.62 0.85
Cappuzzo et al 2019[21] 1.04(0.63,1.72) 0.73(0.57,0.92) 1.04 0.63 1.72 0.73 0.57 0.92
Borghaei et al 2020[24] 0.8(0.49,1.31) 0.61(0.48,0.79) 0.8 0.49 1.31 0.61 0.48 0.79
Reck M. et al 2021[8] 0.54(0.34,0.85) 0.83(0.65,1.06) 0.54 0.34 0.85 0.83 0.65 1.06
Reck M et al 2021[25] 0.83(0.57,1.2) 0.64(0.51,0.8) 0.83 0.57 1.2 0.64 0.51 0.8
Makoto et al 2021[19] 0.96(0.58,1.58) 0.86(0.69,1.06) 0.96 0.58 1.58 0.86 0.69 1.06
Pooled HR 0.80(0.70,0.92) 0.72(0.67,0.78) 0.8 0.7 0.92 0.72 0.67 0.78

forest<-read.delim("clipboard")

attach(forest)

labeltext <- as.matrix(forest[,1:3])

coef <- with(forest, cbind(hr1, hr2))

low <- with(forest, cbind(ci.lb1, ci.lb2))

high <- with(forest, cbind(ci.ub1, ci.ub2))

library(forestplot)

forestplot(labeltext, mean = coef, lower = low, upper = high, zero = 1, lwd.zero = 2,graph.pos = 2,boxsize = 0.25,align = "l", col=fpColors(box=c("#B30638", "#00549E")),lwd.xaxis=2)

What should I do? :pleading_face: :pleading_face:

The last few posts in the following thread show how to store text in a data frame that can be used to display superscripts in a plot. Is that a solution to your problem? A data frame cannot store formatted text, as far as I know.

Due to the limitations of reprex the script will not render a^1 as the output, but it will appear in the RStudio viewer pane when copied and run.

library(katex)
library(stringr)

# create a data frame 
x <- c("a1", "b2")
y <- c("c3", "d4")
# avoid naming objects after built-in functions
# like c, df, data, etc. as an easy way of 
# avoiding namespace conflicts that can cause
# an error
# view it
dat <- data.frame(x, y)
# write a function to convert values to 
# superscripted versions for the case of
# a single lower-case letter with one or
# more integers in either order a1 or 1a
script_up <- function(x) {
  the_letter = "[a-z]"
  the_number = "[0-9]+"
  letter_part = str_extract(x,the_letter)
  number_part = str_extract(x,the_number)
  katex_html(paste0(letter_part,"^",number_part),include_css = TRUE)
}

script_up(dat[1,1])
a1a^1a1

Thanks for your kindness. Actually i attempt to insert references in a forestplot and superscript the numbers. Now it seems better not to do this in a dataframe. Would you please be so kind to give me a solution? Here is part of my data and codes, the numbers[23], [22], [9], [21] in the first column is the inserted references:

hr1 ci.lb1 ci.ub1 hr2 ci.lb2 ci.ub2
Author HR(95%CI)
LM non-LM
Louis 2016[23] 0.64(0.37,1.1) 0.68(0.49,0.94) 0.64 0.37 1.1 0.68 0.49 0.94
Achim 2017[22] 0.82(0.59,1.14) 0.72(0.6,0.88) 0.82 0.59 1.14 0.72 0.6 0.88
Ramalingam et al 2019[9] 0.82(0.62,1.08) 0.72(0.62,0.85) 0.82 0.62 1.08 0.72 0.62 0.85
Cappuzzo et al 2019[21] 1.04(0.63,1.72) 0.73(0.57,0.92) 1.04 0.63 1.72 0.73 0.57 0.92

forest<-read.delim("clipboard")
attach(forest)

labeltext <- as.matrix(forest[,1:3])

coef <- with(forest, cbind(hr1, hr2))

low <- with(forest, cbind(ci.lb1, ci.lb2))

high <- with(forest, cbind(ci.ub1, ci.ub2))

library(forestplot)

forestplot(labeltext, mean = coef, lower = low, upper = high, zero = 1, lwd.zero = 2,graph.pos = 5,boxsize = 0.25,align = "l", hrzl_lines = list("2" = gpar(lty=1, columns=c(2:7), col = "black"), "3" = gpar(lty=1, columns=c(6:7), col = "black")),is.summary= c(T,T,F,F,F,F,F,F,F,F,F,F,F,F,F,T,F,F,F,F,F,F,F),col=fpColors(box=c("#B30638", "#00549E")),legend=c("LM", "non-LM"), legend_args = fpLegend(pos = list(x =3, y=-0.02),r = unit(.1, "snpc")),txt_gp = fpTxtGp(label = gpar(cex = 1.0,fontfamily="myFont1"),ticks = gpar(cex=1.20),xlab = gpar(cex=1.15)),xlab="<---favor intervention-- --favor control--->",lwd.xaxis=2)

I really appreciate that.Actually i had read those posts and found they were not what i want. I attempt to insert references in a forestplot and superscript the numbers. Now it seems better not to do this in a dataframe. Would you please be so kind to give me a solution? Here is part of my data and codes, the numbers[23], [22], [9], [21] in the first column is the inserted references:

hr1 ci.lb1 ci.ub1 hr2 ci.lb2 ci.ub2
Author HR(95%CI)
LM non-LM
Louis 2016[23] 0.64(0.37,1.1) 0.68(0.49,0.94) 0.64 0.37 1.1 0.68 0.49 0.94
Achim 2017[22] 0.82(0.59,1.14) 0.72(0.6,0.88) 0.82 0.59 1.14 0.72 0.6 0.88
Ramalingam et al 2019[9] 0.82(0.62,1.08) 0.72(0.62,0.85) 0.82 0.62 1.08 0.72 0.62 0.85
Cappuzzo et al 2019[21] 1.04(0.63,1.72) 0.73(0.57,0.92) 1.04 0.63 1.72 0.73 0.57 0.92
Borghaei et al 2020[24] 0.8(0.49,1.31) 0.61(0.48,0.79) 0.8 0.49 1.31 0.61 0.48 0.79
Reck M. et al 2021[8] 0.54(0.34,0.85) 0.83(0.65,1.06) 0.54 0.34 0.85 0.83 0.65 1.06
Reck M et al 2021[25] 0.83(0.57,1.2) 0.64(0.51,0.8) 0.83 0.57 1.2 0.64 0.51 0.8
Makoto et al 2021[19] 0.96(0.58,1.58) 0.86(0.69,1.06) 0.96 0.58 1.58 0.86 0.69 1.06
Pooled HR 0.80(0.70,0.92) 0.72(0.67,0.78) 0.8 0.7 0.92 0.72 0.67 0.78

forest<-read.delim("clipboard")

See the FAQ: How to do a minimal reproducible example reprex for beginners for the preferred way to providing representative data and code.

Sorry about that , i've edited the data and codes on top.

Please see the reprex FAQ--the point is to provide a complete script with enough data to illustrate the problem in a form that can be cut and pasted by the reader so a solution can be proposed and illustrated.

Here's an example

# library(dplyr) # not needed here, but use for any that are
my_matrix <- structure(c(
  NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
  NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
  NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
  NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
  NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
  NA, NA, NA, NA, NA, NA, NA
), .Dim = c(6L, 14L), .Dimnames = list(
  c("P18", "P19", "P20", "P21", "P22", "P23"), c(
    "P35_cor",
    "P35_p", "P36_cor", "P36_p", "P37_cor", "P37_p", "P38_cor",
    "P38_p", "P39_cor", "P39_p", "P40_cor", "P40_p", "P41_cor",
    "P41_p"
  )
))

# odd and even columns

# get indices of columns
get_index <- function(x,y) seq(from = y, to = dim(x)[2],by = 2)

my_matrix[,get_index(my_matrix,1)]
#>     P35_cor P36_cor P37_cor P38_cor P39_cor P40_cor P41_cor
#> P18      NA      NA      NA      NA      NA      NA      NA
#> P19      NA      NA      NA      NA      NA      NA      NA
#> P20      NA      NA      NA      NA      NA      NA      NA
#> P21      NA      NA      NA      NA      NA      NA      NA
#> P22      NA      NA      NA      NA      NA      NA      NA
#> P23      NA      NA      NA      NA      NA      NA      NA

my_matrix[,get_index(my_matrix,2)]
#>     P35_p P36_p P37_p P38_p P39_p P40_p P41_p
#> P18    NA    NA    NA    NA    NA    NA    NA
#> P19    NA    NA    NA    NA    NA    NA    NA
#> P20    NA    NA    NA    NA    NA    NA    NA
#> P21    NA    NA    NA    NA    NA    NA    NA
#> P22    NA    NA    NA    NA    NA    NA    NA
#> P23    NA    NA    NA    NA    NA    NA    NA
forest<-structure(list(Author = c("Richard2012[1]", "Wang2015[2]"), coef = c(0.3, 0.45), low = c(0.1, 0.2), high = c(0.4, 0.55)), class = "data.frame", row.names = c(NA, -2L))
labeltext <- as.matrix(forest[,1])
library(forestplot)
#> 载入需要的程辑包:grid
#> 载入需要的程辑包:magrittr
#> 载入需要的程辑包:checkmate
forestplot(labeltext, mean = coef, lower = low, upper = high)
#> Error in NCOL(lower): 找不到对象'low'

Created on 2022-02-23 by the reprex package (v2.0.1)
Sorry for the late reply :pleading_face:. I clearly assigned values to low, yet there is always"Error: object 'low' can not be found". I was totally confused :persevere: and asked many people without an answer.Is this data appropriate now?

Your variables are inside your forest object, so you need to pick them there. This works for me.

forest<-structure(list(Author = c("Richard2012[1]", "Wang2015[2]"), coef = c(0.3, 0.45), low = c(0.1, 0.2), high = c(0.4, 0.55)), class = "data.frame", row.names = c(NA, -2L))
labeltext <- as.matrix(forest[,1])
library(forestplot)
#> Le chargement a nécessité le package : grid
#> Le chargement a nécessité le package : magrittr
#> Le chargement a nécessité le package : checkmate
forestplot(labeltext, mean = forest$coef, lower = forest$low, upper = forest$high)

Created on 2022-03-04 by the reprex package (v2.0.1)

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

Sure.It turns out to be a stupid question.Thanks! :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.