Search code examples
rdata-visualizationr-forestplot

Forestplot R - expanding plot, edit a variable name


I prepared a code to visualize my data:

    library(forestplot)
test_data <- data.frame(coef=c(1.14, 0.31, 10.70),
                        low=c(1.01, 0.12, 1.14),
                        high=c(1.30, 0.83, 100.16),
                        boxsize=c(0.2, 0.2, 0.2))
row_names <- cbind(c("Variable", "Variable 1", "Variable 2", "So looooooong and nasty name of the variable"),
                   c("OR", test_data$coef), c("CI -95%",  test_data$low), c("CI +95%", test_data$high) )
test_data <- rbind(rep(NA, 4), test_data)
forestplot(labeltext = row_names,
           mean = test_data$coef, upper = test_data$high,
           lower = test_data$low,
           is.summary=c(TRUE, FALSE, FALSE, FALSE),
           boxsize = test_data$boxsize,
           zero = 1,
           xlog = TRUE,
           xlab = "OR (95% CI)",
           col = fpColors(lines="black", box="black"),
           title="My Happy Happy Title \n o happy happy title...\n",
           ci.vertices = TRUE,
           xticks = c(0.1, 1, 10, 100))

It gives a following forestplot:

forestplot

I would like to:

1) expand the plot and diminish font of the plot details on the left for better visualization

2) edit "So looooooong and nasty name of the variable" to move part "name..." below the row like: " So looooooong and nasty name of the variable " However, when I write as "/nSo.../n" it gives another row of number from columns "OR" and "CIs".

How correct it?


Solution

  • Three possibilities (one more than you asked for):

    1) change text of row labels with txt_gp.

    2) cut column spacing from 6 mm default to half that value by passing colgap a grid call to unit. Fully understanding the options for forestplot requires understanding the grid system of plotting.

    3) add a "\n" to the loooong label. (I'm puzzled you didn't see that possibility, since you already had a "\n" in the title.)

    row_names <- cbind(c("Variable", "Variable 1", "Variable 2", "So looooooong and \nnasty name of the variable"),
                   c("OR", test_data$coef), c("CI -95%",  test_data$low), c("CI +95%", test_data$high) )
    
    forestplot(labeltext = row_names,
               mean = test_data$coef, upper = test_data$high,
               lower = test_data$low,
               is.summary=c(TRUE, FALSE, FALSE, FALSE),
               boxsize = test_data$boxsize,
               zero = 1, colgap = unit(3, "mm"), txt_gp=fpTxtGp(label= gpar(cex = 0.7),
                                                                 title = gpar(cex = 1) ),
               xlog = TRUE,
               xlab = "OR (95% CI)",
               col = fpColors(lines="black", box="black"),
               title="My Happy Happy Title \n o happy happy title...\n",
               ci.vertices = TRUE,
               xticks = c(0.1, 1, 10, 100))
    

    enter image description here

    If I only used a cex of 0.7 in the call to gpar passed to 'label', it also affected the size of the title, so I needed to "reset" the 'cex' of the 'title' back to 1.