简体   繁体   中英

Positioning title above forest plot (R - meta package)

The answer https://stackoverflow.com/a/12372998/3513286 shows how one can add a title to a forest plot that was created with R's meta package. But it does so with fixed coordinates. I'd like to position the title relatively to the forest plot because I want to plot several of them and they have different numbers of rows so that with a fixed coordinate the gap between title and plot would not be the same across all these plots.

If I'd have enough reputations points, I'd have asked this in a comment to the mentioned answer. If somebody can move it there: that would be great.

Edit: Two plots with little vs. a lot of space between plot and title:

library(meta)
data(Fleiss93cont)

# little space:
forest(metacont(n.e, mean.e, sd.e, n.c, mean.c, sd.c,
    data=rbind(Fleiss93cont, Fleiss93cont), sm="SMD"))
grid.text("Title", .5, .75, gp=gpar(cex=2))

# a lot of space:
forest(metacont(n.e, mean.e, sd.e, n.c, mean.c, sd.c,
    data=Fleiss93cont[1:2,], sm="SMD"))
grid.text("Title", .5, .75, gp=gpar(cex=2))

As nobody else answered, here my current workaround. This is certainly no good answer, because it uses a lot of non-R programs, but it might be better than nothing.

Here, I export the graphic as pdf, use pdftoppm to convert it into png format, convert to trim it, and read the output of identify back into R to see at what percentage of the original height the upper border of the non-white portion is. This number, multiplied by 1.1, gave me here a fairly good result.

library(meta)
data(Fleiss93cont)

height <- function() {
    dev.copy2pdf(file="tmp.pdf", width=12, height=6)
    system("pdftoppm -png -r 72 tmp.pdf > tmp.png")
    system("convert tmp.png -trim tmp.png")
    system2("identify", "tmp.png", stdout="tmp")
    tmp <- as.character(read.table("tmp")$V4)
    tmp <- as.numeric(unlist(strsplit(tmp, "[x+]")))
    system("rm tmp tmp.pdf tmp.png")
    return( 1-tmp[4]/tmp[2] )
}

# longer table:
forest(metacont(n.e, mean.e, sd.e, n.c, mean.c, sd.c,
    data=rbind(Fleiss93cont, Fleiss93cont), sm="SMD"))
h <- height(); print(h)
grid.text("Title", .5, 1.1*h, gp=gpar(cex=2))
dev.copy2pdf(file="tmp_1.pdf", width=12, height=6)

# shorter table:
forest(metacont(n.e, mean.e, sd.e, n.c, mean.c, sd.c,
    data=Fleiss93cont[1:2,], sm="SMD"))
h <- height(); print(h)
grid.text("Title", .5, 1.1*h, gp=gpar(cex=2))
dev.copy2pdf(file="tmp_2.pdf", width=12, height=6)

A good solution probably requires knowledge of the grid package and/or the forest function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM