How to change linetype in the legend with ggcuminc?

61 Views Asked by At

I am trying to manually work with color and linetype using ggcuminc(). As an example:

library(ggsurvfit)
library(tidycmprsk)

# Create an additional categorical variable to include
df_lung$age_cat <- ifelse(df_lung$age > 63, "GT63", "LTE63")
df_lung$status <- factor(df_lung$status)

# Default
cuminc(Surv(time, status) ~ sex + age_cat, data = df_lung) %>%
  ggcuminc(size = 1.5)

I would like to group the line colors by sex and line types by age category. I tried:

cuminc(Surv(time, status) ~ sex + age_cat, data = df_lung) %>%
  ggcuminc(size = 1.5) +
  scale_color_manual(values = c("black", "red", "black", "red")) +
  scale_linetype_manual(values = c("solid", "solid", "dashed", "dashed"))

which gets the color to change, but not the linetype. After looking at the ggcuminc(), I found that specifying linetype_aes is reflected in the plot, but is not reflected in the legend.

cuminc(Surv(time, status) ~ sex + age_cat, data = df_lung) %>%
  ggcuminc(size = 1.5,
           linetype_aes = TRUE) +
  scale_color_manual(values = c("black", "red", "black", "red")) +
  scale_linetype_manual(values = c("solid", "solid", "dashed", "dashed"))

I then tried using the guide options in scale_linetype_manual to no avail.

cuminc(Surv(time, status) ~ sex + age_cat, data = df_lung) %>%
  ggcuminc(size = 1.5,
           linetype_aes = TRUE) +
  scale_color_manual(values = c("black", "red", "black", "red")) +
  scale_linetype_manual(values = c("solid", "solid", "dashed", "dashed"),
                        guide = guide_legend(override.aes = list(linetype = c("solid", "solid", "dashed", "dashed"))))

The data for the plot can be viewed by

mydata <- cuminc(Surv(time, status) ~ sex + age_cat, data = df_lung)$tidy
View(mydata)

And I can see that the strata column contains the four unique combinations of sex and age category. Is there a way to work with this setup to get the desired legend. I suppose I could create new columns to reflect sex and age category and then build my own ggplot using geom_step, but it would be nice to just be able to specify what I want within ggcuminc().

0

There are 0 best solutions below