How can I change the aesthetic mapping for confidence intervals when using ggcompetingrisks?

Viewed 64

The confidence interval colors don't match the line colors. (picture included. I'm aware the plot is not very informative since I used a small subset of the data for this example.)

ggcompetingrisks() is from the survminer package.

p3 <- ggcompetingrisks(fit = x, 
                       multiple_panels = F, 
                       ggtheme = theme_minimal(),
                       coef = 1.96, #95% CI
                       conf.int = T,
                       xlim = c(0, 15),
                       ylim = c(0, 0.1),
                       xlab = "Days", 
                       ylab = "Probability",
                       title = "Cumulative Incidence by Time Period") +
  theme(plot.title = element_text(hjust = 0.5), #center title
        axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)), #pad y axis label
        axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + #pad x axis label
  scale_color_manual(labels = c("Period 1","Period 2","Period 3"),  
                     values = c("#9ef0f0", "#08bdba", "#0f62fe"), 
                     name = element_blank())   # do not name legend
p3$mapping <- aes(x = time, y = est, colour = group, linetype = event) # overwrite aes to flip linetype and color mappings

p3

It produces this plot.

Below is the code to reproduce the cuminc object for the plot.

x <- 
structure(list(`1 1` = list(time = c(0, 6, 6, 7, 7, 8, 8, 10, 10, 11, 11, 14, 14, 15, 15), 
                            est = c(0, 0, 0.002714939, 0.002714939, 0.025168630, 0.025168630, 0.029488245, 0.029488245, 0.035423797, 0.035423797, 0.04164908, 0.04164908, 0.04553497, 0.04553497, 0.04689997), 
                            var = c(0, 0, 2.452515e-06, 2.452515e-06, 2.289665e-05, 2.289665e-05, 2.734325e-05, 2.734325e-05, 3.402077e-05, 3.402077e-05,4.129261e-05, 4.129261e-05, 4.597699e-05, 4.597699e-05, 4.770832e-05)), 
               
               `2 1` = list(time = c(0, 5, 5, 6, 6, 7, 7, 10, 10, 11, 11, 12, 12, 14, 14), 
                            est = c(0, 0, 0.001256405, 0.001256405, 0.005138794, 0.005138794, 0.013189197, 0.013189197, 0.016127006, 0.016127006,0.02671412, 0.02671412, 0.02829462, 0.02829462, 0.04365371), 
                            var = c(0, 0, 1.578565e-06, 1.578565e-06, 6.577619e-06, 6.577619e-06, 1.719998e-05, 1.719998e-05, 2.140657e-05, 2.140657e-05, 3.681209e-05, 3.681209e-05, 3.918992e-05, 3.918992e-05, 6.379430e-05)), 
               
               `3 1` = list(time = c(0, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,  9,  9, 10, 10), 
                            est = c(0, 0, 0.001532092, 0.001532092, 0.004695122, 0.004695122, 0.007942922, 0.007942922, 0.011258385, 0.011258385, 0.01647932, 0.01647932, 0.01826306, 0.01826306, 0.02565641), 
                            var = c(0,0,2.347337e-06,2.347337e-06,7.326912e-06,7.326912e-06,1.254441e-05,1.254441e-05,1.794701e-05,1.794701e-05, 2.681038e-05, 2.681038e-05, 2.989403e-05, 2.989403e-05, 4.302871e-05)),
              
               Tests = structure(c(74.0658010607094, 89.5877719522838, 3.10862446895044e-15, 0, 4, 4), 
                                 .Dim = 2:3, 
                                 .Dimnames = list(c("1", "2"), c("stat", "pv", "df")))), class = "cuminc") 
1 Answers

It is a bit complicated, but I found out that in the fill column of the data, all the colours are the same. That's why you have the same color for each CI. To prevent that, you can create p3 as a ggplot object using ggplot_build. Next you conditionally replace the values in fill column of the data to the right column. Here is a reproducible example:

x <- 
  structure(list(`1 1` = list(time = c(0, 6, 6, 7, 7, 8, 8, 10, 10, 11, 11, 14, 14, 15, 15), 
                              est = c(0, 0, 0.002714939, 0.002714939, 0.025168630, 0.025168630, 0.029488245, 0.029488245, 0.035423797, 0.035423797, 0.04164908, 0.04164908, 0.04553497, 0.04553497, 0.04689997), 
                              var = c(0, 0, 2.452515e-06, 2.452515e-06, 2.289665e-05, 2.289665e-05, 2.734325e-05, 2.734325e-05, 3.402077e-05, 3.402077e-05,4.129261e-05, 4.129261e-05, 4.597699e-05, 4.597699e-05, 4.770832e-05)), 
                 
                 `2 1` = list(time = c(0, 5, 5, 6, 6, 7, 7, 10, 10, 11, 11, 12, 12, 14, 14), 
                              est = c(0, 0, 0.001256405, 0.001256405, 0.005138794, 0.005138794, 0.013189197, 0.013189197, 0.016127006, 0.016127006,0.02671412, 0.02671412, 0.02829462, 0.02829462, 0.04365371), 
                              var = c(0, 0, 1.578565e-06, 1.578565e-06, 6.577619e-06, 6.577619e-06, 1.719998e-05, 1.719998e-05, 2.140657e-05, 2.140657e-05, 3.681209e-05, 3.681209e-05, 3.918992e-05, 3.918992e-05, 6.379430e-05)), 
                 
                 `3 1` = list(time = c(0, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,  9,  9, 10, 10), 
                              est = c(0, 0, 0.001532092, 0.001532092, 0.004695122, 0.004695122, 0.007942922, 0.007942922, 0.011258385, 0.011258385, 0.01647932, 0.01647932, 0.01826306, 0.01826306, 0.02565641), 
                              var = c(0,0,2.347337e-06,2.347337e-06,7.326912e-06,7.326912e-06,1.254441e-05,1.254441e-05,1.794701e-05,1.794701e-05, 2.681038e-05, 2.681038e-05, 2.989403e-05, 2.989403e-05, 4.302871e-05)),
                 
                 Tests = structure(c(74.0658010607094, 89.5877719522838, 3.10862446895044e-15, 0, 4, 4), 
                                   .Dim = 2:3, 
                                   .Dimnames = list(c("1", "2"), c("stat", "pv", "df")))), class = "cuminc") 

library(survminer)
library(dplyr)
library(ggplot2)
p3 <- ggcompetingrisks(fit = x, 
                       multiple_panels = F, 
                       ggtheme = theme_minimal(),
                       coef = 1.96, #95% CI
                       conf.int = T,
                       xlim = c(0, 15),
                       ylim = c(0, 0.1),
                       xlab = "Days", 
                       ylab = "Probability",
                       title = "Cumulative Incidence by Time Period") +
  theme(plot.title = element_text(hjust = 0.5), #center title
        axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)), #pad y axis label
        axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + #pad x axis label
  scale_color_manual(labels = c("Period 1","Period 2","Period 3"),  
                     values = c("#9ef0f0", "#08bdba", "#0f62fe"), 
                     name = element_blank()) +   # do not name legend
  scale_fill_manual(labels = c("Period 1"),  
                    values = c("#9ef0f0", "#08bdba", "#0f62fe"))
p3$mapping <- aes(x = time, y = est, colour = group, linetype = event, fill = event) # overwrite aes to flip linetype and color mappings

p3

p_build <- ggplot_build(p3)

p_build$data[[2]]$fill
#>  [1] "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0"
#>  [8] "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0"
#> [15] "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0"
#> [22] "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0"
#> [29] "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0"
#> [36] "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0" "#9ef0f0"
#> [43] "#9ef0f0" "#9ef0f0" "#9ef0f0"

p_build$data[[1]] <- p_build$data[[1]] %>% mutate(fill = case_when(colour == "#9ef0f0" ~ "#9ef0f0",
                                                                   colour == "#08bdba" ~ "#08bdba",
                                                                   colour == "#0f62fe" ~ "#0f62fe"))


grid::grid.draw(ggplot_gtable(p_build))

Created on 2022-07-14 by the reprex package (v2.0.1)

Related