我正在为两个不同人群绘制三种不同类型癌症的生存图。我希望每种癌症类型都有一种颜色,人口之间的差异应为线型(虚线与实线)。因此,癌症A为红色,PopA为虚线,PopB为实线,依此类推。
我似乎无法为每个癌症群体获得一种颜色,然后改变人群之间的线型。
由于受保护,因此我无法在此处发布所有数据,但这是df,surv函数和ggsurvplot:
library(tidyverse)
library(survminer)
library(survival)
library(KMsurv)
library(ggfortify)
library(ggthemes)
> srv_time_mon <- sample(0:100,200, replace = TRUE)
> cancer <- sample(1:3,200, replace = TRUE)
> alive <- sample(0:1,200, replace = TRUE)
> pop <- sample(1:2,200, replace = TRUE)
> dead <- sample(0:1,200, replace = TRUE)
> df <- data.frame(srv_time_mon,cancer, alive, pop, dead)
> head(df)
srv_time_mon cancer alive pop dead
1 84 1 0 1 1
2 91 2 0 2 0
3 44 1 0 2 1
4 57 2 1 1 0
5 54 1 1 2 0
6 31 3 0 2 1
test <- survfit(Surv(srv_time_mon, dead)~pop+cancer, data=df)
ggsurvplot(test, data=df, conf.int = FALSE,
pval = "Log-Rank p <0.001", title="Kaplan-Meier Curves", break.time.by=12,
risk.table = TRUE,
pval.coord=c(156,0.60), surv.median.line = "v",
legend.labs=c("Pop A Cancer 1", "Pop A Cancer 2", "Pop A Cancer 3", "Pop B Cancer 1",
"Pop B Cancer 2", "Pop B Cancer 3"),
legend.title="", legend = c(0.8,0.83), linetype = c("pop"), palette = "jco")+
scale_linetype_discrete( name="Pop Data", labels=c("A", "B"))
答案 0 :(得分:3)
如果您查看attachments
返回的对象的class
,我们将其称为ggsurvplot()
,您会发现它不是p
对象
ggplot
您将在此处找到class(p)
#[1] "ggsurvplot" "ggsurv" "list"
对象
ggplot
如果您现在运行
class(p$plot)
#[1] "gg" "ggplot"
您会看到以下情节:
现在,如果您想要一个图例,请将p$plot + scale_linetype_discrete(name = "Pop Data", labels = c("A", "B"))
调用中的linetype
参数更改为ggsurvplot()
,以便将两个图例组合在一起,因为现在同一变量已映射到不同的外观。
最后将"strata"
和scale_linetype_manual()
添加到scale_colour_manual()
。
请参阅下面的完整代码。
p$plot
我假设您希望2型和3型癌症的颜色为黑色。如果您想要不同的颜色,请在下面更改代码。
p <- ggsurvplot(
test,
data = df,
conf.int = FALSE,
pval = "Log-Rank p <0.001",
title = "Kaplan-Meier Curves",
break.time.by = 12,
risk.table = TRUE,
pval.coord = c(156, 0.60),
surv.median.line = "v",
legend.labs = c(
"Pop A Cancer 1",
"Pop A Cancer 2",
"Pop A Cancer 3",
"Pop B Cancer 1",
"Pop B Cancer 2",
"Pop B Cancer 3"
),
legend.title = "",
# legend = c(0.8, 0.83), # took this out for cleaner appearance of the legend (optional)
linetype = c("strata"), # changed from "pop" to "strata"
palette = "jco"
)
使用patchwork
package在图表下方添加风险表(另一个cols <- rep(c("red", "black"), c(2, 4))
#cols <- rep(c("red", "forestgreen", "lightblue"), 2) # example
names(cols) <- paste("Pop", rep(c("A", "B"), each = 3), "Cancer", 1:3)
lines <- rep(c("dashed", "solid"), each = 3)
names(lines) <- paste("Pop", rep(c("A", "B"), each = 3), "Cancer", 1:3)
p$plot <- p$plot +
scale_linetype_manual(values = lines) +
scale_colour_manual(values = cols) +
theme(legend.position = "bottom")
对象)。
ggplot
数据
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
p$plot / p$table + plot_layout(ncol = 1, heights = c(3, 1)) +
theme(axis.text.y = element_text(color = rev(cols)))