简体   繁体   English

R生存曲线图图例

[英]R Survival Curve Plot Legend

I have a table that looks like this:我有一张看起来像这样的表:

ID Survival Event Allele
2   5   1   WildType
2   0   1   WildType
3   3   1   WildType
4   38  0   Variant

I want to do a kaplan meier plot, and tell me if wild type or variants tend to survive longer.我想做一个卡普兰迈尔图,并告诉我野生型或变体是否倾向于存活更长时间。

I have this code:我有这个代码:

library(survival)
Table <-read.table("Table1",header=T)
fit=survfit(Surv(Table$Survival,Table$Event)~Table$Allele)
plot(fit,lty=2:3,col=3:4)

From the fit p value, I can see that the survival of these two groups have significantly different survival curves.从拟合的p值可以看出,这两组的生存率有着明显不同的生存曲线。

survdiff(formula = Surv(dat$Death, dat$Event) ~ dat$Allele, rho = 0)
#                            N Observed Expected (O-E)^2/E (O-E)^2/V 
#    dat$Allele=Variant   5592     3400     3503      3.00      8.63
#    dat$Allele=WildType  3232     2056     1953      5.39      8.63
#    Chisq= 8.6  on 1 degrees of freedom, p= 0.0033

The plot looks as expected (ie two curves).该图看起来如预期(即两条曲线)。

All I want to do is put a legend on the plot, so that I can see which data is represented by the black and red lines, ie do the Wild Type or Variant survive longer.我想要做的就是在情节上放一个图例,以便我可以看到哪些数据由黑线和红线表示,即 Wild Type 或 Variant 的存活时间更长。

I have tried these two commands:我试过这两个命令:

lab <-gsub("x=","",names(fit$strata))
legend("top",legend=lab,col=3:4,lty=2:3,horiz=FALSE,bty='n')

The first command works (ie I get no error).第一个命令有效(即我没有出错)。 The second command, I get this error:第二个命令,我收到此错误:

Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet strwidth(legend, units = "user", cex = cex, font = text.font) 中的错误:尚未调用 plot.new

I've tried reading forums etc., but none of the answers seem to work for me (for example, changing between top/topright/topleft etc. doesn't matter).我试过阅读论坛等,但似乎没有一个答案对我有用(例如,在顶部/右上角/左上角等之间切换并不重要)。

Edit 1: This is an example of a table for which I get this error:编辑 1:这是我收到此错误的表的示例:

    ID Survival Event Allele
25808   5   1   WTHomo
22196   0   1   Variant
22518   3   1   Variant
25013   38  0   Variant
27354   5   1   Variant
27223   4   1   Variant
22700   5   1   Variant
22390   24  1   Variant
17586   1   1   Variant

What exactly happens is: when I type the very last command ( legend("top",legend=lab,col=3:4,lty=2:3,horiz=FALSE,bty='n')), the XII window opens, except it's completely blank.究竟发生了什么:当我输入最后一个命令时(legend("top",legend=lab,col=3:4,lty=2:3,horiz=FALSE,bty='n')),XII 窗口打开,除了它完全空白。

But then if you just type "plot(fit,lty=2:3,col=3:4)", the XII window and the plot appear.但是如果你只输入“plot(fit,lty=2:3,col=3:4)”,XII 窗口和绘图就会出现。

Edit 2: Also, this graph will have two lines, how do I tell which line is which variable?编辑 2:此外,该图将有两条线,我如何判断哪条线是哪个变量? Would the easiest way to do this be to type summary(fit) which gives me two tables.最简单的方法是输入 summary(fit) 这给我两个表格。 Then, whichever variable comes first in the table, I put in first in the legend?那么,无论哪个变量首先出现在表中,我都会首先放在图例中?

Many thanks Eva非常感谢伊娃

I too have had repeated problems with the "plot.new has not been called yet" error!我也遇到过“plot.new 尚未被调用”错误的重复问题! Strangely, the error was intermittent and repeating the identical commands did not always result in the error!奇怪的是,错误是间歇性的,重复相同的命令并不总是导致错误! In my case, I found that by preceding the plotting command with就我而言,我发现通过在绘图命令之前加上

plot.new()

stopped the error from appearing!阻止错误出现! I have no idea why.我不知道为什么。 Just as an aside, I also had no problem adding a legend to the survival plot using your command.顺便说一句,我也可以使用您的命令向生存图添加图例。

You can also do this using ggsurvplot() from survminer .您也可以使用来自survminer ggsurvplot()来做到这survminer Here is an example这是一个例子

library(survminer) # Contains ggsurvplot()
library(survival) # Contains survfit()
ggsurvplot(
  fit=survfit(Surv(time, censor) ~ Allele, data=your_data,type="kaplan-meier"), # Model
  xlab="Years",
  ylab="Overall survival probability",
  legend.labs=c("WildType","Variant"), # Assign names to groups which are shown in the plot
  conf.int = T, # Adds a 95%-confidence interval
  pval = T, # Displays the P-value in the plot
  pval.method = T # Shows the statistical method used for obtaining the P-value
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM