简体   繁体   English

R:如何在生存图中自动设置不同组的颜色

[英]R: How to automatically set the color of different groups in survival plot

I am plotting the survival probability for my dataframe with 8 different groups with this command: 我使用以下命令绘制具有8个不同组的数据框的生存概率:

fit2<-Surv((time=t2$uptimeDay,event=t2$solved,type='right')~t2$cluster)

plot(fit2,conf.int=F,xlim=c(0, 250),mark.time=c(1,50,100,200),mark=c(1,3,4,2,5,7,6,8,9,10),lwd=1,cex=0.7,lty = 1:11,xlab='Time(days)',ylab='Survival Probability')

the cluster here is a number between 1 and 10. 这里的簇是1到10之间的数字。

I would like to know how to automatically set the colors of the curves together with an automatic legend using key of the curves. 我想知道如何使用曲线键自动设置曲线的颜色以及自动图例。

Can somebody help me out with this? 有人可以帮我这个忙吗?

I have a function that I use for Kaplan-Meier curves that is based on ggplot2 , which will take care of the colors and legends for you. 我有一个用于基于ggplot2 Kaplan-Meier曲线的ggplot2 ,它将为您处理颜色和图例。 Regrettably, I've not gotten around to packaging it up in any sensible way. 遗憾的是,我还没有以任何明智的方式来打包它。 But you can download the source code from 但是您可以从以下位置下载源代码

https://gist.github.com/nutterb/004ade595ec6932a0c29 https://gist.github.com/nutterb/004ade595ec6932a0c29

And some examples on how to use it from 还有一些有关如何使用它的示例

https://gist.github.com/nutterb/fb19644cc18c4e64d12a https://gist.github.com/nutterb/fb19644cc18c4e64d12a

It's not clear what you mean by making this "automatic" and the desire to "use the key of the curves", but perhaps you are asking that the colors of the curves match the legend. 不清楚使“自动”功能和“使用曲线关键点”的含义是什么意思,但也许您是在要求曲线的颜色与图例匹配。

png()
  mycols=c("red","blue")
  plot(prio.fit, fill=mycols)
  legend(x="bottomleft", col=mycols, legend=mycols) 
dev.off()

在此处输入图片说明

If you want this mated to a dataset and wanted to specify particular colors for your groups, then you will need to provide a dataset so there is something meaningful to use as labels, and be more specific about the coloring schema needed. 如果要将其与数据集配对,并希望为组指定特定的颜色,则需要提供一个数据集,以便将一些有意义的内容用作标签,并更具体地说明所需的着色方案。

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

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