1
t <- c(1,1,2,2,3,4,4,5,5,6,6,6,6,7,8,8,8,8,9,10,10,11,11,11,12,12,13,15,16,17,17,19,
       20,22,22,23,23,25,32,32,34,35)
status <-c(1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1,0,0,1,1,1,1,0,0,0,0,0)
grupo <- c(1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0)
sex <- c(1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,1,1,1,1,1,1,1)
data<- data.frame(t,status,grupo, sex)

# Fit a Cox model 
library(survival)
fit <- coxph(Surv(t,status) ~ grupo+sex, data=data, method='breslow')

# Predict Survival
datosnuevos <- data.frame(grupo=c(0,0,1,1), sex=c(0,1,0,1))  
curva <- survfit(fit, newdata = datosnuevos, conf.type = "log-log")
df<- data.frame(time=curva$time, TtoFemale=curva$surv[,1], TtoMale=curva$surv[,2],
                PboFemale=curva$surv[,3], PboMale=curva$surv[,4])

# log minus log (survival)
library(ggplot2)
ggplot()+
  geom_step(aes(time, log(-log(TtoFemale)), col='TTo0_Female0'), lty=2, data=df)+
  geom_step(aes(time, log(-log(TtoMale)), col='TTo0_Male1'),lty=2, data=df)+
  geom_step(aes(time, log(-log(PboFemale)), col='Pbo1_Female0'), data=df)+
  geom_step(aes(time, log(-log(PboMale)), col='Pbo1_Male1'), data=df)+
  labs(color= 'TRT-Sex', x = "time", y = "log(-log Survival)", title = "")+
  scale_color_manual(name = "TRT-Sex", values = c("TTo0_Female0" = "pink", "TTo0_Male1" = "blue",
                    "Pbo1_Female0" = "pink", "Pbo1_Male1" = "blue"))+
  theme_classic()+
  theme(legend.position = "right", legend.direction = "vertical") +
  theme(legend.title = element_text(family = "Courier", color = "black", size = 12,  face = 1))+
  theme(legend.text = element_text (size = 10)) +
  scale_linetype_manual(name = "TRT-Sex", values=c(2,2,1,1))

enter image description here

How can I modify the two first lines in the legend (must be 'dotted line') I have tried it with -- scale_linetype_manual(name = "TRT-Sex", values=c(2,2,1,1)) --- but It doesn't work... Thanks

2 Answers2

2

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.
Then only one call to geom_step will plot all 4 lines and the legend automatically draws the lines with the same type as the graph since it inherits the linetype aesthetic.

library(ggplot2)

df |>
  tidyr::pivot_longer(cols = -time, names_to = "TRT-Sex") |> 
  ggplot(aes(time, log(-log(value)))) +
  geom_step(aes(color = `TRT-Sex`, linetype = `TRT-Sex`), size = 1) +
  labs(color = "TRT-Sex", x = "time", y = "log(-log Survival)", title = "")+
  scale_color_manual(name = "TRT-Sex", 
                     values = c(TtoFemale = "pink", TtoMale = "blue",
                                PboFemale = "pink", PboMale = "blue")) +
  scale_linetype_manual(name = "TRT-Sex", 
                        values = c(TtoFemale = "dashed", TtoMale = "dashed",
                                   PboFemale = "solid", PboMale = "solid")) +
  theme_classic(base_size = 16) +
  theme(legend.position = "right", legend.direction = "vertical",
        #legend.title = element_text(family = "Courier", color = "black", size = 12,  face = 1),
        legend.text = element_text (size = 10),
        legend.key.width = unit(2, "line"))

Created on 2022-10-10 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
1

You can use

guides(color = guide_legend(override.aes = list(linetype = c(2,2,1,1))))

t <- c(1,1,2,2,3,4,4,5,5,6,6,6,6,7,8,8,8,8,9,10,10,11,11,11,12,12,13,15,16,17,17,19,
       20,22,22,23,23,25,32,32,34,35)
status <-c(1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1,0,0,1,1,1,1,0,0,0,0,0)
grupo <- c(1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,0,0,0,0,0,0)
sex <- c(1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,1,1,1,1,1,1,1)
data<- data.frame(t,status,grupo, sex)

# Fit a Cox model 
library(survival)

fit <- coxph(Surv(t,status) ~ grupo+sex, data=data, method='breslow')

# Predict Survival
datosnuevos <- data.frame(grupo=c(0,0,1,1), sex=c(0,1,0,1))  
curva <- survfit(fit, newdata = datosnuevos, conf.type = "log-log")
df<- data.frame(time=curva$time, TtoFemale=curva$surv[,1], TtoMale=curva$surv[,2],
                PboFemale=curva$surv[,3], PboMale=curva$surv[,4])

# log minus log (survival)
library(ggplot2)

ggplot()+
  geom_step(aes(time, log(-log(TtoFemale)), col='TTo0_Female0'), lty=2, data=df)+
  geom_step(aes(time, log(-log(TtoMale)), col='TTo0_Male1'),lty=2, data=df)+
  geom_step(aes(time, log(-log(PboFemale)), col='Pbo1_Female0'), data=df)+
  geom_step(aes(time, log(-log(PboMale)), col='Pbo1_Male1'), data=df)+
  labs(color= 'TRT-Sex', x = "time", y = "log(-log Survival)", title = "")+
  scale_color_manual(name = "TRT-Sex", values = c("TTo0_Female0" = "pink", 
                                                  "TTo0_Male1" = "blue",
                                                  "Pbo1_Female0" = "pink", 
                                                  "Pbo1_Male1" = "blue"))+
  
  scale_linetype_manual(name = "TRT-Sex", values=c(2,2,1,1))+
  #new
  guides(color = guide_legend(override.aes = list(linetype = c(2,2,1,1))))+
  #
  theme_classic()+
  theme(legend.position = "right", 
        legend.direction = "vertical",
        legend.title = element_text(family = "Courier", color = "black", size = 12,  face = 1),
        legend.text = element_text (size = 10))

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

Bruno Mioto
  • 382
  • 1
  • 10