Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upggsurvplot removes points when a named vector is passed as a palette #100
Comments
|
To add to this, it looks like I also need to ensure that my groups are in the correct order when I pass them to survfit, for them to match up correctly. It might also be good if the names on the palette would match to the group (e.g. in this example, for sex==1 the colour would be the one named "1" on the palette.) |
|
Oh, hmmm... It actually looks like it automatically sorts the group names into alphabetical order, then assigns them to the palette. I have one data set with a list of clusters from This can be hacked around by alphabetically reordering the palette, but that's a little convoluted. Probably the ideal behaviour would be for it to respect the order of factor levels where the grouping variable is a factor. I believe that's how ggplot handles colours, and would allow for custom ordering of groups by manipulating the order of the factor levels. |
|
Hi @oneillkza, Thank you for reporting these behaviors of ggsurvplot. However, I think that many of these behaviors seem to be caused by the way the data is prepared. It's not a problem in ggsurvplot. A) In the lung data, by default the levels of the library(survival)
library(survminer)
# Create the variable sex 2
data("lung")
lung$sex2 <- factor(lung$sex, levels = c(2, 1))
# Fit survival curves for the variable sex
fit1<- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit1, risk.table = TRUE)# Fit survival curves for the variable sex2
fit2<- survfit(Surv(time, status) ~ sex2, data = lung)
ggsurvplot(fit2, risk.table = TRUE)Pease, make sure that your cluster variable is a factor with levels = 1:11, before fitting survival curves. If this is not the case, the default behavior of mydata$clusters <- factor(mydata$clusters, levels = 1:11)
fit <-survfit(time, status)~ clusters, data = mydata)
ggsurvplot(fit)B) Color palettes In For example: ggsurvplot(fit2, risk.table = TRUE, palette = c("black", "lightgray"))This default behavior of ggplot2-based plotting system can be changed by assigning correctly a named vector. That is, in our example, the names of colors should match the names of strata as generated by the For example, now, I want to color the sex2=2 in lightgray and sex2=1 in black: pals = c("black", "lightgray")
names(pals) <- paste0("sex2=", c(1, 2))
ggsurvplot(fit2, risk.table = TRUE, palette = pals)Please let me know if it works with your data. Best regards, |
|
Thanks for the detailed and thoughtful answer! After some poking, that does actually work for me. I hadn't realised it had to match the strata names, or that survfit derived those from the formula. This might be useful information to include in the documentation for Also, interestingly, it did not work when I used $ notation in the formula passed to survfit (such that the strata names contained $). |
|
I agree with you and I updated the documentation . In About the dollar ($) notation, we generally prefer when users separate the formula and the data as follow (case 1): library(survival)
fit1 <- survfit(Surv(time, status) ~ sex, data = lung)Instead of using this (case 2): library(survival)
fit2 <- survfit(Surv(lung$time, lung$status) ~ lung$sex)However, even if we don't recommend the script used in the case 2, the current version of For example: # Default plot
library(survminer)
ggsurvplot(fit2, risk.table = TRUE, pval = TRUE, conf.int = TRUE)# Change color palette (1/2): sex=1 in "black"; sex=2 in "grey"
pals <- c("black", "grey")
names(pals) <- c("sex=1", "sex=2")
ggsurvplot(fit2, risk.table = TRUE, pval = TRUE, conf.int = TRUE,
palette = pals)# Change color palette (2/2): sex=1 in "grey"; sex=2 in "black"
pals <- c("black", "grey")
names(pals) <- c("sex=2", "sex=1")
ggsurvplot(fit2, risk.table = TRUE, pval = TRUE, conf.int = TRUE,
palette = pals)As you mentioned, in the case 2, strata names returned by Let me know if it's ok for you so that we can close this issue. Have a good day, |
|
Looks good to me! Thanks so much for all your hard work! |







Expected behavior
Survival plot with points, using the provided palette.
Actual behavior
Steps to reproduce the problem
Note: this is mostly just an annoyance, but I have a use case where I defined a palette for another plot, and wanted the colours in my survival plot to match. It was fairly confusing trying to trace this, and it is slightly annoying having to copy the palette to a new variable and scrub the names before it works.
session_info()