Search code examples
rr-forestplot

Add header to a forestplot diagram


I'm trying to add a header to the forestplot. But I keep getting the following error:

Error in pr_convert_insert_estimates(mean = mean, lower = lower, upper = upper,  : 
  Label length is not equal to values2 != 1

Can anyone please help?

Here is the code that I have used:

pacman::p_load(forestplot, tidyverse, dplyr)

labeltext <- c('Altered level of consciousness','Central neuropathy','Fever','Headache','Hypotension','Nausea vomiting','Pallor','Visual changes','Weakness','Altered level of consciousness','Central neuropathy','Fever',
               'Headache','Hypotension','Nausea vomiting','Pallor','Visual changes','Weakness')

mean <- c(0.52,0.88,1.01,0.97,0.55 ,0.99,0.64,0.49,NA,NA,0.04 ,0.1,0.09,0.04,0.04,0.04 ,0.04,NA)
lower <-c(0.39,0.7,0.72,0.59,0.45,0.69,0.44,0.32,NA,NA,0.02,0.04,0.03,0.02,0.01,0.01,0.01,NA)
upper <-c(0.68,1.11,1.43,1.6,0.68,1.42,0.95,0.73,NA,NA,0.11,0.23,0.31,0.09,0.15,0.16,0.17,NA)

df <- data.frame(labeltext, mean, lower, upper)

df %>% 
  forestplot(title = "Model estimates",
             labeltext = c(labeltext, mean, lower, upper),
             clip = c(0,3),
             lty.ci=c(3),
             lwd.ci = 1,
             ci.vertices = TRUE,
             ci.vertices.height = 0.05,
             boxsize = .25,
             ticks = gpar(fontfamily = "", cex = 5),
             xlab = "mean (95% CI)",
             grid = structure(c(1), 
             gp=gpar(lty=2, lwd=1))) %>% 
    fp_add_lines() %>% 
    fp_set_style(box = "royalblue",
                 line = "darkblue",
                 align = "lrrr",
                 hrz_lines = "#999999") %>% 
      fp_add_header(labeltext = c("", "study variables"),
                    mean = c("", "Odds Ratio"),
                    lower = c("", "95% LCI"),
                    upper = c("", "95% UCI"))

The above code produces the forestplot that I want except for the header when ran above fp_add_header. I just couldn't get the fp_add_header to work properly. Please help.


Solution

  • I think your problem has to do with using function names like mean to change the header. I have made copies of those columns with mutate and gave them some dummy names. Adopting fp_add_header accordingly made it then possible to add those headers.

    library(forestplot)
    library(dplyr)
    
    labeltext <- c('Altered level of consciousness','Central neuropathy','Fever','Headache','Hypotension','Nausea vomiting','Pallor','Visual changes','Weakness','Altered level of consciousness','Central neuropathy','Fever',
                   'Headache','Hypotension','Nausea vomiting','Pallor','Visual changes','Weakness')
    
    mean <- c(0.52,0.88,1.01,0.97,0.55 ,0.99,0.64,0.49,NA,NA,0.04 ,0.1,0.09,0.04,0.04,0.04 ,0.04,NA)
    lower <-c(0.39,0.7,0.72,0.59,0.45,0.69,0.44,0.32,NA,NA,0.02,0.04,0.03,0.02,0.01,0.01,0.01,NA)
    upper <-c(0.68,1.11,1.43,1.6,0.68,1.42,0.95,0.73,NA,NA,0.11,0.23,0.31,0.09,0.15,0.16,0.17,NA)
    
    df <- data.frame(labeltext, mean , lower, upper)
    
    df %>% 
      # create copies 
      mutate(mm =mean, ll = lower, uu = upper) %>% 
      forestplot(title = "Model estimates",
                 labeltext = c(labeltext, mm, ll, uu),
                 clip = c(0,3),
                 lty.ci=c(3),
                 lwd.ci = 1,
                 ci.vertices = TRUE,
                 ci.vertices.height = 0.05,
                 boxsize = .25,
                 ticks = gpar(fontfamily = "", cex = 5),
                 xlab = "mean (95% CI)",
                 grid = structure(c(1), 
                                  gp=gpar(lty=2, lwd=1))) %>% 
      fp_add_lines() %>% 
      fp_set_style(box = "royalblue",
                   line = "darkblue",
                   align = "lrrr",
                   hrz_lines = "#999999") %>% 
      fp_add_header(labeltext =  c("", "study variables"),
                    mm = c("","Odds"),
                    ll = c("", "95% LCI"),
                    uu = c("", "95% UCI"))