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
我想绘制一个kaplan meier图,并告诉我野生型或变异型是否倾向于存活更长的时间.
I want to do a kaplan meier plot, and tell me if wild type or variants tend to survive longer.
我有此代码:
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)
从拟合p值可以看出,这两组的生存曲线具有显着不同.
From the fit p value, I can see that the survival of these two groups have significantly different survival curves.
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 (i.e. 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, i.e. do the Wild Type or Variant survive longer.
我尝试了以下两个命令:
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 (i.e. I get no error). The second command, I get this error:
strwidth错误(旧版,单位=用户",cex = cex,字体= text.font): plot.new尚未被调用
Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet
我曾尝试阅读论坛等内容,但似乎没有任何答案对我有用(例如,在top/topright/topleft等之间切换无所谓).
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:
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
实际发生的是:当我键入最后一条命令时(legend("top",legend = lab,col = 3:4,lty = 2:3,horiz = FALSE,bty ='n')), XII窗口将打开,但它完全空白.
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.
但是,如果您只键入"plot(fit,lty = 2:3,col = 3:4)",则会显示XII窗口和图.
But then if you just type "plot(fit,lty=2:3,col=3:4)", the XII window and the plot appear.
此外,此图将有两条线,我如何分辨哪条线是哪个变量?最简单的方法是键入summary(fit),这给了我两个表.然后,无论哪个变量在表中排在第一位,我在图例中排在第一位?
Edit 2: Also, this graph will have two lines, how do I tell which line is which variable? Would the easiest way to do this be to type summary(fit) which gives me two tables. Then, whichever variable comes first in the table, I put in first in the legend?
非常感谢 伊娃
推荐答案
我也屡次遇到"plot.new还没有被调用"错误!奇怪的是,错误是间歇性的,重复相同的命令并不总是会导致错误!就我而言,我发现在plotting命令之前加上
I too have had repeated problems with the "plot.new has not been called yet" error! 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.
这篇关于R生存曲线图图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!