Adjust margin for forestplot, footnotes - r

I'm trying to use the forestplot and I want to include a lengthy footnote (3 lines of text). I can't get enough space at the bottom and I don't think the par() options are doing anything. Here's the example given in the vingette along with my attempt at making a footnote:
library(forestplot)
# Cochrane data from the 'rmeta'-package
cochrane_from_rmeta <-
structure(list(
mean = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531),
lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA, 0.731)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -11L),
class = "data.frame")
tabletext<-cbind(
c("", "Study", "Auckland", "Block",
"Doran", "Gamsu", "Morrison", "Papageorgiou",
"Tauesch", NA, "Summary"),
c("Deaths", "(steroid)", "36", "1",
"4", "14", "3", "1",
"8", NA, NA),
c("Deaths", "(placebo)", "60", "5",
"11", "20", "7", "7",
"10", NA, NA),
c("", "OR", "0.58", "0.16",
"0.25", "0.70", "0.35", "0.14",
"1.02", NA, "0.53"))
dev.new()
par(mar = c(4, 1, 1, 1))
forestplot(tabletext,
cochrane_from_rmeta,new_page = TRUE,
is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE),
clip=c(0.1,2.5),
xlog=TRUE,
col=fpColors(box="royalblue",line="darkblue", summary="royalblue"))
grid.text('* Adjusted for demographic & baseline variables,blur blur blur',
x = unit(.43, 'npc'),
y = unit(1, 'lines'))
Any suggestions on how to create some more space?
Thanks!

I find a solution, first I though that I could change the margine thanks to the ggplot option of the theme :
+ theme(plot.margin = margin(2, 2, 2, 2, "cm"))
But it was not possible to have access to the plot object in forestplot, so I look closly the option of forestplot function and there is an argument mar which take units as parameter, the following solution works for me :
forestplot(tabletext,
cochrane_from_rmeta,new_page = TRUE,
is.summary=c(TRUE, TRUE, rep(FALSE,8), TRUE),
clip=c(0.1, 2.5),
xlog=TRUE,
mar = unit(rep(10, times = 4), "mm"),
col=fpColors(box="royalblue",
line="darkblue",
summary="royalblue"))
grid.text('* Adjusted for demographic & baseline variables,blur blur blur',
x = unit(.43, 'npc'),
y = unit(1, 'lines'))

Related

How can I add another row of text to the bottom of a forest plot made with forestplot?

I have created a forest plot using the forestplot package:
As you can see, the list of heterogeneity statistics in the final row of the first column is quite long, and it widens the column. I would like to remove this text from the labeltext argument (where I put it to generate the above plot, making the first column wider) and draw it once, separately, so it appears where it is but it extends under the other columns without making the first one wider.
I know I can use something like this to draw what I am looking for:
grid.text("test", x=unit(?????), y=unit(?????), rot=0,
gp=gpar(col="red"))
How can I get the location of the start of the first cell of the final row, underneath 'Overall'?
Is there some way I can get the width of the entire plot so I can then find x by subtracting half the width from the middle of the viewport?
If anybody knows a better way of going about this, please let me know.
You could use grid.text where you need to specify npc in the units of the grid like this:
library(forestplot)
cochrane_from_rmeta <- structure(list(mean = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531),
lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA, 0.731)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -11L),
class = "data.frame")
tabletext <- cbind(c("", "Study", "Auckland", "Block", "Doran", "Gamsu", "Morrison", "Papageorgiou", "Tauesch", NA, "Summary"),
c("Deaths", "(steroid)", "36", "1", "4", "14", "3", "1", "8", NA, NA),
c("Deaths", "(placebo)", "60", "5", "11", "20", "7", "7", "10", NA, NA),
c("", "OR", "0.58", "0.16", "0.25", "0.70", "0.35", "0.14", "1.02", NA, "0.53"))
forestplot(cochrane_from_rmeta, labeltext = tabletext,
is.summary = c(rep(TRUE, 2), rep(FALSE, 8), TRUE),
clip = c(0.1, 2.5),
xlog = TRUE,
col = fpColors(box = "royalblue",
line = "darkblue",
summary = "royalblue"))
x <- unit(.05, 'npc')
y <- unit(.05, 'npc')
grid.text('A test text', x, y, gp = gpar(fontsize=10, font = 3))
Created on 2022-07-17 by the reprex package (v2.0.1)

Making single element bold in forestplot

I want to make certain text elements bold in my forestplot column text. This is very similar to this question, but slightly different because I only want one item bolded.
Using a combination of expression(bold()) I have been able to do so:
library(forestplot)
cochrane_from_rmeta <-
structure(list(
mean = c(NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017),
lower = c(NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365),
upper = c(NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -8L),
class = "data.frame")
tabletext <- cbind(
c("Study", "Auckland", "Block",
"Doran", "Gamsu", "Morrison", "Papageorgiou",
"Tauesch"),
c("Deaths", "36", "1",
"4", "14", "3", "1",
"8"),
c("OR", "0.58", "0.16",
"0.25", "0.70", "0.35", "0.14",
"1.02"))
tabletext_lists <- list(list(), list(), list())
tabletext_lists[[1]] <- tabletext[,1]
tabletext_lists[[2]] <- tabletext[,2]
tabletext_lists[[3]] <- tabletext[,3]
tabletext_lists[[3]][3] <- list(expression(bold("0.16")))
tabletext_lists[[3]][7] <- list(expression(bold(tabletext_lists[[3]][7])))
forestplot(tabletext_lists,
cochrane_from_rmeta,new_page = TRUE,
clip = c(0.1,2.5),
xlog = TRUE,
col = fpColors(box = c(rep("royalblue", 5), "red", "red"),
line = "darkblue"))
Which yields this figure:
I cannot figure out how to make the item of interest appear bold without manually entering it in such as tabletext_lists[[3]][3] <- list(expression(bold("0.16")))
Is there a way to make individual elements bold without using the is.summary parameter from forestplot? And as such is it possible to do so without having to manually enter the text value itself and do so by slicing or normal selection from a vector?
You can use in forestplot the txt_gp option. List1 is a 2-dimensionnal matrix where list[[row]][[column]] is a gpar(fontface="bold") or gpar(fontface="plain").
In the forestplot, it will put as bold or plain the element of interest.
For instance, in your case, list1[[1]][[1]] is for the element Auckland and should be set as plain.
forestplot(tabletext,
txt_gp=(label=fpTxtGp(list1))
)

R, how to change to interval in the y axis of a forest plot?

I'm preparing e a forest plot in R through the forestplot package. The possible values range from -1 to +1, and I would like that to be the range on my x axis. My values, however, range from 0 to 1, so R is depicting only that part on the x axis.
See the image here:
How can I make the x axis interval range from -1 to +1? I want to leave a complete empty space on the right to show no value is there.
Here's my code:
library("forestplot")
cochrane_from_rmeta <-
structure(list(
lower = c(NA, NA, 0.026, 0.043, 0.184, 0.333, 0.026),
mean = c(NA, NA, 0.502, 0.534, 0.548, 0.792, 0.600),
upper = c(NA, NA, 0.978, 0.949, 0.911, 0.936, 0.967)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -7L),
class = "data.frame")
tabletext<-cbind(
c("", "aaa", "bbb", "ccc",
"ddd", "eee",
"Summary"),
c("", "#datasets", "1", "2",
"3", "4",
NA))
png(paste0("forestPlot_",exe_num,".png"))
forestplot(tabletext,
cochrane_from_rmeta,
new_page = TRUE,
is.summary=c(TRUE,TRUE,rep(FALSE,4),TRUE),
clip=c(-1,1),
xlog=FALSE,
col=fpColors(box="royalblue",line="darkblue", summary="royalblue"))
There doesn't seem to be a direct way of doing this, but if you set a lower and upper of -1 and 1 in your first row while leaving mean as NA, it will fix the x range without affecting the plot otherwise:
cochrane_from_rmeta <-
structure(list(
lower = c(-1, NA, 0.043, 0.043, 0.184, 0.333, 0.026),
mean = c(NA, NA, 0.502, 0.534, 0.548, 0.792, 0.600),
upper = c(1, NA, 0.978, 0.949, 0.911, 0.936, 0.967)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -7L),
class = "data.frame")
tabletext<-cbind(
c("", "aaa", "bbb", "ccc",
"ddd", "eee",
"Summary"),
c("", "#datasets", "1", "2",
"3", "4",
NA))
forestplot(tabletext,
cochrane_from_rmeta,
new_page = TRUE,
is.summary=c(FALSE,TRUE,rep(FALSE,4),TRUE),
clip=c(-1,1),
xlog=FALSE,
mar = unit(c(50, 30, -30, 30), "pt"),
col=fpColors(box="royalblue",line="darkblue", summary="royalblue"),
graphwidth = unit(0.7, "npc"))

Modify font of single element in forestplot

I'm trying to modify make a single element in labeltext italics without making the entire column italics, with no luck. The following code will make the 1 italics:
library(forestplot)
Cochrane data from the 'rmeta'-package
cochrane_from_rmeta <-
structure(list(
mean = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531),
lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA,
0.731)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -11L),
class = "data.frame")
tabletext<-cbind(
c("", "Study", "Auckland", "Block",
"Doran", "Gamsu", "Morrison", "Papageorgiou",
"Tauesch", NA, "Summary"),
c("Deaths", "(steroid)", "36", "1",
"4", "14", "3", "1",
"8", NA, NA),
c("Deaths", "(placebo)", "60", "5",
"11", "20", "7", "7",
"10", NA, NA),
c("", "OR", "0.58", "0.16",
"0.25", "0.70", "0.35", "0.14",
"1.02", NA, "0.53"))
forestplot(tabletext,
txt_gp = fpTxtGp(label = list(gpar(fontface = 3),
gpar(fontface = 1,
)),
ticks = gpar(fontfamily = "", cex=1),
xlab = gpar(fontfamily = "HersheySerif", cex = 1.5)),
rbind(HRQoL$Sweden),
col=clrs,
xlab="EQ-5D index")
But how do I modify the script to only make "Male vs Female" italics (i.e. element 1,1 of label text).
More details on the function can be found here: https://cran.r-project.org/web/packages/forestplot/forestplot.pdf
There is only support to style individual columns with the fpTxtGp for this at the moment but you can do a hack that gives you the same result if you don't have any summary elements in your table (from the vignette):
forestplot(tabletext,
boxsize = .05,
is.summary=c(FALSE, FALSE, TRUE, FALSE),
txt_gp = fpTxtGp(summary = list(
gpar(fontfamily = "HersheyScript", fontface=1),
gpar(fontface=1)
)
),
rbind(HRQoL$Sweden),
col=clrs,
fn.ci_sum=function(col, size, ...) {
fpDrawNormalCI(clr.line = col, clr.marker = col, size=.05, ...)
},
xlab="EQ-5D index")

Forestplot in R superscript in tabletext matrix

I am creating a forestplot in R. Since I cannot display my own data I am using example code and data that I found here
https://cran.r-project.org/web/packages/forestplot/vignettes/forestplot.html
cochrane_from_rmeta <-
structure(list(
mean = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531),
lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA, 0.731)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -11L),
class = "data.frame")
tabletext<-cbind(
c("", "Study", "Auckland", "Block",
"Doran", "Gamsu", "Morrison", "Papageorgiou",
"Tauesch", NA, "Summary"),
c("Deaths", "(steroid)", "36", "1",
"4", "14", "3", "1",
"8", NA, NA),
c("Deaths", "(placebo)", "60", "5",
"11", "20", "7", "7",
"10", NA, NA),
c("", "OR", "0.58", "0.16",
"0.25", "0.70", "0.35", "0.14",
"1.02", NA, "0.53"),
c("",NA,NA,NA,NA,NA,NA,NA,NA,NA,"Heterogeniety I^2 = 20%"))[enter image description here][1]
library(forestplot)
forestplot(tabletext,
cochrane_from_rmeta,new_page = TRUE,
is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE),
clip=c(0.1,2.5),
xlog=TRUE,
col=fpColors(box="royalblue",line="darkblue", summary="royalblue"))
I would like to superscript the very last label for Heterogeniety. I tried expression(Heterogeniety~I^2), but I get the following error message:
Error in cbind(c("Outcome", "Death or BPD", NA, NA, NA, "Interaction p value = .xx", : cannot create a matrix from type 'expression'
How do I include a superscript in my forestplot?
R does not allow you to create a matrix of expression. Also mentioned in the documentation for forestplot,
You can also provide a matrix although this cannot have expressions by design
Luckily, the documentation for forestplot also mentioned that it can take in a list for labeltext.
The list should be wrapped in m x n number to resemble a matrix: list(list("rowname 1 col 1", "rowname 2 col 1"), list("r1c2", expression(beta))
Hence, you might want to convert your tabletext into a nested list as follows:
tabletext <- list(
list("", "Study", "Auckland", "Block",
"Doran", "Gamsu", "Morrison", "Papageorgiou",
"Tauesch", NA, "Summary"),
list("Deaths", "(steroid)", "36", "1",
"4", "14", "3", "1",
"8", NA, NA),
list("Deaths", "(placebo)", "60", "5",
"11", "20", "7", "7",
"10", NA, NA),
list("", "OR", "0.58", "0.16",
"0.25", "0.70", "0.35", "0.14",
"1.02", NA, "0.53"),
list("",NA,NA,NA,NA,NA,NA,NA,NA,NA, expression(Heterogeniety~I^2==20~'%')))
forestplot(tabletext,
cochrane_from_rmeta,new_page = TRUE,
is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE),
clip=c(0.1,2.5),
xlog=TRUE,
col=fpColors(box="royalblue",line="darkblue", summary="royalblue"))
HTH.

Resources