Martin Hennessy
Martin Hennessy

Reputation: 55

What is wrong with my custom colour palette in this plot?

Using ggsurvplot to draw some Kaplan-Meier curves.

5 curves should be plotted and I want control over their colours.

Here is the output of the survfit being plotted:

> elective_30Decadesurv

Call: survfit(formula = elective30Surv ~ electives$Decade)

                      n events median 0.95LCL 0.95UCL
electives$Decade=50  14      0     NA      NA      NA
electives$Decade=60 173      2     NA      NA      NA
electives$Decade=70 442      5     NA      NA      NA
electives$Decade=80 168      4     NA      NA      NA
electives$Decade=90   2      0     NA      NA      NA

Here is a working plot using the default colour palette, "hue":

> ggsurvplot(elective_30Decadesurv,
         data = electives,
         palette = "hue",
         title = "30 day survival after elective EVAR",
         legend = "none",
         legend.title = "Decade",
         legend.labs = c("5th",
                         "6th",
                         "7th",
                         "8th",
                         "9th"
                         ),
         censor.shape = 124,
         ggtheme = survPlotTheme,
         risk.table = "nrisk_cumevents",
         risk.table.y.text.col = TRUE,
         risk.table.fontsize = 3,
         risk.table.height = 0.3,
         break.time.by = 5,
         ylim = c(0.95,
                  1
                  ),
         pval = TRUE,
         pval.size = 3,
         pval.coord = c(1,
                        0.96
                        )
         )

See plot in section 3.1.4 of this webpage for the output of the above

The Decade group has 5 entries, so I'm trying to provide five colours to palette.

However, both:

> ggsurvplot(elective_30Decadesurv,
         data = electives,
         palette = c("#440154",
                     "#3B528B",
                     "#21908C",
                     "#5DC863",
                     "#5DC863"
                     ),
         title = "30 day survival after elective EVAR",
         legend = "none",
         legend.title = "Decade",
         legend.labs = c("5th",
                         "6th",
                         "7th",
                         "8th",
                         "9th"
                         ),
         censor.shape = 124,
         ggtheme = survPlotTheme,
         risk.table = "nrisk_cumevents",
         risk.table.y.text.col = TRUE,
         risk.table.fontsize = 3,
         risk.table.height = 0.3,
         break.time.by = 5,
         ylim = c(0.95,
                  1
                  ),
         pval = TRUE,
         pval.size = 3,
         pval.coord = c(1,
                        0.96
                        )
         )

And:

> fiveColours <- c("#440154",
                   "#3B528B",
                   "#21908C",
                   "#5DC863",
                   "#5DC863"
                   )

> ggsurvplot(elective_30Decadesurv,
         data = electives,
         palette = fiveColours,
         title = "30 day survival after elective EVAR",
         legend = "none",
         legend.title = "Decade",
         legend.labs = c("5th",
                         "6th",
                         "7th",
                         "8th",
                         "9th"
                         ),
         censor.shape = 124,
         ggtheme = survPlotTheme,
         risk.table = "nrisk_cumevents",
         risk.table.y.text.col = TRUE,
         risk.table.fontsize = 3,
         risk.table.height = 0.3,
         break.time.by = 5,
         ylim = c(0.95,
                  1
                  ),
         pval = TRUE,
         pval.size = 3,
         pval.coord = c(1,
                        0.96
                        )
         )

Give the same error:

Error in names(.cols) <- grp.levels : 
  'names' attribute [5] must be the same length as the vector [4]

What vector is length [4]?

Is 'names' attribute my colour vector?

If I take one of the colours out of the custom palette, eg fiveColours <- c("#440154","#3B528B","#21908C","#5DC863") I get this error:

Error: Insufficient values in manual scale. 5 needed but only 4 provided.

Which implies the number of colours provided is correct but something else is causing the issue.

I've troubleshot to the limits of my own ability. Help please!


FYI:

> electives %>% select(Decade) %>% group_by(Decade) %>% summarise(n())
# A tibble: 5 x 2
  Decade `n()`
  <fct>  <int>
1 50        14
2 60       173
3 70       442
4 80       168
5 90         2

Should prove the length of the Decade variable and here is how the survival object and survfit were generated:

> elective5Surv        <- Surv(electives$surv5Y,   electives$dead5Y)
> elective_5Decadesurv <- survfit(elective5Surv  ~ electives$Decade)

Upvotes: 1

Views: 799

Answers (1)

Martin Hennessy
Martin Hennessy

Reputation: 55

Ok, I have sorted my own mistake by proof-reading!

Of the five hex colours I’d provided, two were identical (not on purpose.)

I changed the fifth colour to a different hex value (what it was meant to be in the first place) and it works now.

Thanks, Rui, for your response earlier, it helped me down the path!

Upvotes: 1

Related Questions