R "forestplot" package: Error message when using "group_by" ! missing value where TRUE/FALSE needed

85 views Asked by At

I'm using the forestplot package (https://cran.r-project.org/web/packages/forestplot/vignettes/forestplot.html) for an observational cohort study. I am trying to show the forestplot of the hazard ratios for each outcome with 95%CI and the numbers as text next to the graph. I have managed to make individual results for each group using the below code:

library(forestplot)
library(dplyr)

AAA_smoker_HR_2 <- tibble::tibble(mean  = c(1.23, 0.90, 0.86),
                            lower = c(0.87, 0.87, 0.81),
                            upper = c(1.73, 0.94, 0.92),
                            var = c("In-Hospital Mortality",
                                    "Length of Hospital Stay",
                                    "Length of ITU Stay"),
                            HR = c("1.23 (0.87-1.73)", "0.90 (0.87-0.94)", "0.86 (0.81-0.92)"))

AAA_smoker_HR_2 |>
  forestplot(labeltext = c(var, HR),
             fn.ci_norm = fpDrawDiamondCI,
             zero = 1,
             clip = c(0.0, 2.0),
             boxsize = 0.18,
             vertices = TRUE,
             ci.vertices.height = 0.05,
             lineheight = "lines",
             xlab = "Hazard Ratio (HR)",
             xticks = c(0.5, 1.0, 1.5, 2.0)
             ) |>
  fp_add_lines() |> 
  fp_set_style(box = "black",
               line = "black",
               hrz_lines = "#999999",
               txt_gp = fpTxtGp(ticks=gpar(cex=0.75),
                              label=gpar(cex=0.9),
                              xlab=gpar(cex=0.9))) |> 
  fp_add_header(var = c("", "Variable"),
                HR = c("", "HR (95% CI)")) |>
  fp_decorate_graph(graph.pos = 2)

This graph is generated

I have 3 subgroups in total (All, OSR, and EVAR) and created respective data frames for each. I am now trying to create a forestplot that will include all three subgroups as per Max Gordon's instructions and using dplyr to merge into one data frame. The code is below:

OSR_smoker_HR_2 <- tibble::tibble(mean  = c(1.18, 1.00, 0.96),
                            lower = c(0.87, 0.97, 0.91),
                            upper = c(1.60, 1.04, 1.01),
                            var = c("In-Hospital Mortality",
                                    "Length of Hospital Stay",
                                    "Length of ITU Stay"),
                            HR = c("1.18 (0.87-1.60)", "1.00 (0.97-1.04)", "0.96 (0.91-1.01)"))

EVAR_smoker_HR_2 <- tibble::tibble(mean  = c(1.22, 0.98, 0.92),
                            lower = c(0.82, 0.91, 0.85),
                            upper = c(1.82, 1.04, 0.99),
                            var = c("In-Hospital Mortality",
                                    "Length of Hospital Stay",
                                    "Length of ITU Stay"),
                            HR = c("1.22 (0.82-1.82)", "0.98 (0.91-1.04)", "0.92 (0.85-0.99)"))

AAA_J_smoker_HR <- bind_rows(lst(AAA_smoker_HR_2, OSR_smoker_HR_2, EVAR_smoker_HR_2), .id = 'study')

AAA_J_smoker_HR <- AAA_J_smoker_HR %>%
  mutate(study = case_when(
    study == "AAA_smoker_HR_2" ~ "All",
    study == "OSR_smoker_HR_2" ~ "OSR",
    study == "EVAR_smoker_HR_2" ~ "EVAR"
  ))

AAA_J_smoker_HR |>
  group_by(study) |>
  forestplot(labeltext = c(var, HR),
             fn.ci_norm = c(fpDrawDiamondCI, fpDrawNormalCI, fpDrawCircleCI),
             zero = 1,
             clip = c(0.0, 3.0),
             boxsize = 0.18,
             vertices = TRUE,
             ci.vertices.height = 0.05,
             lineheight = "lines",
             xlab = "Hazard Ratio (HR)",
             xticks = c(0.5, 1.0, 1.5, 2.0, 3.0)
             ) |>
  fp_add_lines() |> 
  fp_set_style(box = c("black","blue","darkred"),
               line = "black",
               hrz_lines = "#999999",
               txt_gp = fpTxtGp(ticks=gpar(cex=0.75),
                              label=gpar(cex=0.9),
                              xlab=gpar(cex=0.9))|> lapply(function(x) gpar(fill = x, col = "#555555")),
               default = gpar(vertices=TRUE)) |> 
  fp_add_header(var = c("", "Variable"),
                HR = c("", "HR (95% CI)")) |>
  fp_decorate_graph(graph.pos = 2)

However, I get the error message:

Error in `dplyr::mutate()`:
ℹ In argument: `data = lapply(...)`.
ℹ In group 1: `.fp_groups = "All"`.
Caused by error in `if (df$.fp_labels[i] != all_labels[i]) ...`:
! missing value where TRUE/FALSE needed
Backtrace:
  1. forestplot::fp_decorate_graph(...)
 18. base::lapply(...)
 19. forestplot (local) FUN(X[[i]], ...)

When I use filter(group == "OSR") and without lapply the code still works to generate a single graph, but not when using group_by().

Any help at all is much appreciated!!

I'm not sure if it's the dataframe formatting causing an issue as there are no NA values... and picking out the groups individually works with the code. So I'm at a loss!

0

There are 0 answers