R语言forestplot包labeltext文本左对齐设置求助
Hey there! I totally get the frustration—center-aligned negative values can make your forest plot look really cluttered. The good news is this is an easy fix with the align parameter in the forestplot() function.
Here's what you need to do:
Map your columns: From your code, it looks like your
tabletexthas 7 columns (since your horizontal bands targetc(1:7)), with the graph positioned at column 5 (graph.pos=5). The default alignment for all text columns is center, which is causing the messy layout with negative values.Set column-specific alignment: The
alignargument takes a vector where each element corresponds to a column intabletext, using"left","center", or"right". For the columns holding your effect values (like group difference, lower/upper CI), set those to"left"instead of the default.
For example, if columns 6 and 7 are your numeric effect value columns, add this to your forestplot() call:
align = c("left", "left", "left", "left", "center", "left", "left")
This keeps your descriptive label columns (1-4) left-aligned, the graph column (5) centered, and your numeric value columns (6-7) left-aligned—no more jumbled negative numbers!
- Full modified code: Here's how your updated code will look with the alignment fix:
forestplot( labeltext = tabletext, graph.pos = 5, mean = c(NA, NA, NA, NA, data$Group.difference), lower = c(NA, NA, NA, NA, data$Lower), upper = c(NA, NA, NA, NA, data$Upper), title = "Sensitivity Analyses of gait speed 3 months after Stroke", xlab = "<---RELAX Better--- ---PHYS Better--->", grid = TRUE, hrzl_lines = list( "5" = gpar(lwd=1, col="black"), "9" = gpar(lwd=45, lineend="butt", columns=c(1:7), col="#99999922"), "18" = gpar(lwd=45, lineend="butt", columns=c(1:7), col="#99999922"), "26" = gpar(lwd=45, lineend="butt", columns=c(1:7), col="#99999922") ), txt_gp = fpTxtGp( label = gpar(cex=1.25), ticks = gpar(cex=1.1), xlab = gpar(cex = 1.2), title = gpar(cex = 1.8) ), col = fpColors(box="black", lines="black", zero = "gray50"), zero = 0, cex = 0.9, lineheight = "auto", boxsize = 0.5, colgap = unit(6,"mm"), lwd.ci = 2, ci.vertices = TRUE, ci.vertices.height = 0.4, align = c("left", "left", "left", "left", "center", "left", "left") # Alignment fix added here )
If your numeric columns are in different positions (e.g., columns 4 and 6), just adjust the align vector to set those positions to "left" instead. This should clean up your plot's layout immediately.
Hope this solves your problem! 😊
内容的提问来源于stack exchange,提问作者Torsten

