[R] problem with the legend when having two plots

David Winsemius dwinsemius at comcast.net
Thu Jun 11 16:54:51 CEST 2009


On Jun 11, 2009, at 9:23 AM, kayj wrote:

>
> I am trying to plot survival curves and here is the code
>
> plot(survfit(Surv(days,status)~group, data=g3),
> lty=1:2, mark.time=F,
> ylab="Probability",
> xlab="Suvival Time in Month")
> legend(10, 0.2,legend=c("Control","RIT"), lty=c(2,1),
> title="Hormonal Therapy", bty="n")
>
> I will get two survival plots with the Control on the top of the RIT
>
>
> but if I switch to legend=c(“RIT”,”Control”), I will have the same  
> plots but
> now the top on is named as RIT and the bottom as Control!
>
> It does not seem that R can tell which graph corresponds to what, am I
> missing something.

I think it more that R and the legend function does not do semantic  
checking of plotting activities that preceded it and cannot read  
minds.  You seem to be attributing cognitive capacity to R. Tempting  
for the magical thinking inclined (as we all are to various degrees),  
but not helpful in understanding what a computer does at this stage in  
silicon evolution.

The survfit.object had names for the strata already and you simply  
overwrote that name in the legend. It does not have the facility to  
check to see if the names you supplied reversed any of the internal  
names. You are supposed to know in what order your curves are labeled,  
and if you do not, it is a simple matter to use str() on the survfit  
object. Your approach (reversing both label and line-type should have  
changed only the order that the legend entries appeared  ...  which  
would seem to be useful ability available to you, the wetware operator.

>
> Thanks for the help
>
> -- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list