生存分析surminer包

---
title: "surminer"
output: html_document
---

#生存分析
```{r}
#参考http://www.sthda.com/english/wiki/survminer-r-package-survival-data-analysis-and-visualization
library("survival")
library("survminer")
```


#Draw survival curves without grouping
```{r}
# Fit survival curves
fit <- survfit(Surv(time, status) ~ 1, data = lung)
# Drawing curves
ggsurvplot(fit, color = "#2E9FDF")
```


#Draw survival curves with two groups
```{r}
# Fit survival curves
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# Drawing survival curves
ggsurvplot(fit)
```


#Change font size, style and color
```{r}
# Change font style, size and color
# Change only font size
ggsurvplot(fit, main = "Survival curve",
   font.main = 18,
   font.x =  16,
   font.y = 16,
   font.tickslab = 14)

# Change font size, style and color at the same time
ggsurvplot(fit, main = "Survival curve",
   font.main = c(16, "bold", "darkblue"),
   font.x = c(14, "bold.italic", "red"),
   font.y = c(14, "bold.italic", "darkred"),
   font.tickslab = c(12, "plain", "darkgreen"))

# Change the legend title and labels
ggsurvplot(fit, legend = "bottom",
           legend.title = "Sex",
           legend.labs = c("Male", "Female"))

# Specify legend position by its coordinates
ggsurvplot(fit, legend = c(0.2, 0.2))
```

#Change line types and color palettes
```{r}
# change line size --> 1
# Change line types by groups (i.e. "strata")
# and change color palette
ggsurvplot(fit,  size = 1,  # change line size
           linetype = "strata", # change line type by groups
           break.time.by = 250, # break time axis by 250
           palette = c("#E7B800", "#2E9FDF"), # custom color palette
           conf.int = TRUE, # Add confidence interval
           pval = TRUE # Add p-value
           )

# Use brewer color palette "Dark2"
ggsurvplot(fit, linetype = "strata",
           conf.int = TRUE, pval = TRUE,
           palette = "Dark2")

# Use grey palette
ggsurvplot(fit, linetype = "strata",
           conf.int = TRUE, pval = TRUE,
           palette = "grey")
```

#Add number at risk table
```{r}
# Add risk table
# and change risk table y text colors by strata
ggsurvplot(fit, pval = TRUE, conf.int = TRUE,
           risk.table = TRUE, risk.table.y.text.col = TRUE)

# Customize the output and then print
res <- ggsurvplot(fit, pval = TRUE, conf.int = TRUE,
           risk.table = TRUE)
res$table <- res$table + theme(axis.line = element_blank())
res$plot <- res$plot + labs(title = "Survival Curves")
print(res)

# Change color, linetype by strata, risk.table color by strata
ggsurvplot(fit,
           pval = TRUE, conf.int = TRUE,
           risk.table = TRUE, # Add risk table
           risk.table.col = "strata", # Change risk table color by groups
           linetype = "strata", # Change line type by groups
           ggtheme = theme_bw(), # Change ggplot2 theme
           palette = c("#E7B800", "#2E9FDF"))
```

#Change x axis limits
```{r}
# Change x axis limits (xlim)
#++++++++++++++++++++++++++++++++++++
# One would like to cut axes at a specific time point
ggsurvplot(fit,
           pval = TRUE, conf.int = TRUE,
           risk.table = TRUE, # Add risk table
           risk.table.col = "strata", # Change risk table color by groups
           ggtheme = theme_bw(), # Change ggplot2 theme
           palette = "Dark2",
           xlim = c(0, 600))
```

#Transform survival curves: plot cumulative events and hazard function
```{r}
# Plot cumulative events
ggsurvplot(fit, conf.int = TRUE,
           palette = c("#FF9E29", "#86AA00"),
           risk.table = TRUE, risk.table.col = "strata",
           fun = "event")

# Plot the cumulative hazard function
ggsurvplot(fit, conf.int = TRUE,
           palette = c("#FF9E29", "#86AA00"),
           risk.table = TRUE, risk.table.col = "strata",
           fun = "cumhaz")

# Arbitrary function
ggsurvplot(fit, conf.int = TRUE,
          palette = c("#FF9E29", "#86AA00"),
           risk.table = TRUE, risk.table.col = "strata",
           pval = TRUE,
           fun = function(y) y*100)
```

#Survival curves with multiple groups
```{r}
# Fit (complexe) survival curves
#++++++++++++++++++++++++++++++++++++

fit2 <- survfit( Surv(time, status) ~ rx + adhere,
    data = colon )

# Visualize
#++++++++++++++++++++++++++++++++++++

# Visualize: add p-value, chang y limits
# change color using brewer palette
ggsurvplot(fit2, pval = TRUE,
           break.time.by = 800,
           risk.table = TRUE,
           risk.table.height = 0.5#Useful when you have multiple groups
           )

# Adjust risk table and survival plot heights
# ++++++++++++++++++++++++++++++++++++
# Risk table height
ggsurvplot(fit2, pval = TRUE,
          break.time.by = 800,
          risk.table = TRUE,
          risk.table.col = "strata",
          risk.table.height = 0.5,
          palette = "Dark2")

# Change legend labels
# ++++++++++++++++++++++++++++++++++++

ggsurvplot(fit2, pval = TRUE,
           break.time.by = 800,
           risk.table = TRUE,
           risk.table.col = "strata",
           risk.table.height = 0.5,
           ggtheme = theme_bw(),
           legend.labs = c("A", "B", "C", "D", "E", "F"))
```

评论

此博客中的热门博文

V2ray websocket(ws)+tls+nginx分流

Rstudio 使用代理