Hello,
Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: plot(fit1, col=1:2) lines(fit2,col=1:2) Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing Can anyone have a strategy to make this kind of plot happen? Thanks, John ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi John,
Perhaps the most direct way would be: plot(fit1, col=1:2) xylim<-par("usr") clip(4,xylim[2],xylim[3],xylim[4]) lines(fit2,col=1:2) Remember that the new clipping rectangle will persist until you or something else resets it. Jim On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help <[hidden email]> wrote: > > Hello, > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > plot(fit1, col=1:2) > lines(fit2,col=1:2) > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > Can anyone have a strategy to make this kind of plot happen? > > Thanks, > > John > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by R help mailing list-2
Hi John,
From the looks of the first plot, it would appear that perhaps you are engaged in a landmark analysis in an oncology setting, with the landmark time set at 5 years. On the off chance that you are not familiar with the pros and cons of that methodology, Google "landmark analysis", which should yield a number of references. With respect to the plot itself, here is one approach, in addition to the one that Jim suggested. This will use a 1 x 2 matrix of plot regions, and adjust the various axes accordingly. library(survival) ## Create two models with the same data for this use, the second adding 150 to the event times ## to mimic a landmark time at 150 weeks fit <- survfit(Surv(time, status) ~ x, data = aml) fit2 <- survfit(Surv(time, status) ~ x, data = aml) fit2$time <- fit2$time + 150 ## create the x 1 x 2 plot matrix par(mfrow = c(1, 2)) ## Set the plot region margins so there is no space to the right par(mar = c(4, 4, 4, 0)) ## Fix the plot limits for consistency ## xaxs = "i" removes the default 4% extensions to the plot region limits plot(fit, axes = FALSE, xlim = c(0, 150), ylim = c(0, 1), xaxs = "i") axis(1, at = seq(0, 150, 50), line = -1) axis(2, las = 1) ## Set the plot region margins so there is no space to the left par(mar = c(4, 0, 4, 4)) ## Set the plot limits for the second time interval plot(fit2, axes = FALSE, xlim = c(150, 300), ylim = c(0, 1), xaxs = "i") axis(1, at = seq(150, 300, 50), line = -1) axis(4, las = 1) ## Draw the vertical line at 150 weeks axis(2, at = seq(0, 1, 0.2), labels = FALSE, lty = "dashed") Regards, Marc Schwartz > On Sep 28, 2020, at 8:33 PM, array chip via R-help <[hidden email]> wrote: > > Hello, > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > plot(fit1, col=1:2) > lines(fit2,col=1:2) > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > Can anyone have a strategy to make this kind of plot happen? > > Thanks, > > John > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by Jim Lemon-4
Thank you very much Jim, this is great!!
John On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: Hi John, Perhaps the most direct way would be: plot(fit1, col=1:2) xylim<-par("usr") clip(4,xylim[2],xylim[3],xylim[4]) lines(fit2,col=1:2) Remember that the new clipping rectangle will persist until you or something else resets it. Jim On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help <[hidden email]> wrote: > > Hello, > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > plot(fit1, col=1:2) > lines(fit2,col=1:2) > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > Can anyone have a strategy to make this kind of plot happen? > > Thanks, > > John > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by R help mailing list-2
Thank you Marc as well! I'll try both ways! Yes this is an oncology study with time set at 5
John On Tuesday, September 29, 2020, 07:08:39 AM PDT, Marc Schwartz <[hidden email]> wrote: Hi John, From the looks of the first plot, it would appear that perhaps you are engaged in a landmark analysis in an oncology setting, with the landmark time set at 5 years. On the off chance that you are not familiar with the pros and cons of that methodology, Google "landmark analysis", which should yield a number of references. With respect to the plot itself, here is one approach, in addition to the one that Jim suggested. This will use a 1 x 2 matrix of plot regions, and adjust the various axes accordingly. library(survival) ## Create two models with the same data for this use, the second adding 150 to the event times ## to mimic a landmark time at 150 weeks fit <- survfit(Surv(time, status) ~ x, data = aml) fit2 <- survfit(Surv(time, status) ~ x, data = aml) fit2$time <- fit2$time + 150 ## create the x 1 x 2 plot matrix par(mfrow = c(1, 2)) ## Set the plot region margins so there is no space to the right par(mar = c(4, 4, 4, 0)) ## Fix the plot limits for consistency ## xaxs = "i" removes the default 4% extensions to the plot region limits plot(fit, axes = FALSE, xlim = c(0, 150), ylim = c(0, 1), xaxs = "i") axis(1, at = seq(0, 150, 50), line = -1) axis(2, las = 1) ## Set the plot region margins so there is no space to the left par(mar = c(4, 0, 4, 4)) ## Set the plot limits for the second time interval plot(fit2, axes = FALSE, xlim = c(150, 300), ylim = c(0, 1), xaxs = "i") axis(1, at = seq(150, 300, 50), line = -1) axis(4, las = 1) ## Draw the vertical line at 150 weeks axis(2, at = seq(0, 1, 0.2), labels = FALSE, lty = "dashed") Regards, Marc Schwartz > On Sep 28, 2020, at 8:33 PM, array chip via R-help <[hidden email]> wrote: > > Hello, > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > plot(fit1, col=1:2) > lines(fit2,col=1:2) > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > Can anyone have a strategy to make this kind of plot happen? > > Thanks, > > John > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by Jim Lemon-4
Hi Jim,
I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: ovarian1<-ovarian ovarian1$fustat[ovarian$futime>450]<-0 ovarian1$futime[ovarian$futime>450]<-450 ovarian2<-subset(ovarian,futime>450) fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) plot(fit1, xlim=c(0,1200), col = 1:2) abline(v=450) xylim<-par("usr") clip(450,xylim[2],xylim[3],xylim[4]) lines(fit2, col = 3:4,lty=2) I can still see that the extra horizontal line on the top. Can you or anyone have any suggestion what went wrong? Thanks, John On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: Hi John, Perhaps the most direct way would be: plot(fit1, col=1:2) xylim<-par("usr") clip(4,xylim[2],xylim[3],xylim[4]) lines(fit2,col=1:2) Remember that the new clipping rectangle will persist until you or something else resets it. Jim On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help <[hidden email]> wrote: > > Hello, > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > plot(fit1, col=1:2) > lines(fit2,col=1:2) > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > Can anyone have a strategy to make this kind of plot happen? > > Thanks, > > John > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi John,
I should have remembered this. For some reason, the clip() function doesn't operate until you have issued a graphics command. Try: points(-1,-1) before calling lines() Jim On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > Hi Jim, > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > ovarian1<-ovarian > ovarian1$fustat[ovarian$futime>450]<-0 > ovarian1$futime[ovarian$futime>450]<-450 > > ovarian2<-subset(ovarian,futime>450) > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > plot(fit1, xlim=c(0,1200), col = 1:2) > abline(v=450) > xylim<-par("usr") > clip(450,xylim[2],xylim[3],xylim[4]) > lines(fit2, col = 3:4,lty=2) > > I can still see that the extra horizontal line on the top. > > Can you or anyone have any suggestion what went wrong? > > Thanks, > > John > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > Hi John, > Perhaps the most direct way would be: > > plot(fit1, col=1:2) > xylim<-par("usr") > clip(4,xylim[2],xylim[3],xylim[4]) > lines(fit2,col=1:2) > > Remember that the new clipping rectangle will persist until you or > something else resets it. > > Jim > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > <[hidden email]> wrote: > > > > Hello, > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > plot(fit1, col=1:2) > > lines(fit2,col=1:2) > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > Can anyone have a strategy to make this kind of plot happen? > > > > Thanks, > > > > John > > > > > ______________________________________________ > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi Jim,
I tried points(-1,-1) before lines() and before clip(), but either way, it still shows everything, :-( It's interesting that the examples with hist() provided by the R help of clip function works nicely. I also tried a simple linear regression plots below, clip() works, too. dat<-data.frame(x=1:10,y=1:10) fit<-lm(y~x,dat) plot(1:10) abline(fit) xylim<-par("usr") clip(6,xylim[2],xylim[3],xylim[4]) abline(fit,col=2) ## yes, it only shows the fit line from 6 to the right lines(c(2,8),c(5,5)) ## yes, it only shows the line from 6 to the right So it's puzzling that only when using lines() with a survfit() object (ovarian example below), somehow clip() doesn't work John On Tuesday, September 29, 2020, 07:58:53 PM PDT, Jim Lemon <[hidden email]> wrote: Hi John, I should have remembered this. For some reason, the clip() function doesn't operate until you have issued a graphics command. Try: points(-1,-1) before calling lines() Jim On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > Hi Jim, > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > ovarian1<-ovarian > ovarian1$fustat[ovarian$futime>450]<-0 > ovarian1$futime[ovarian$futime>450]<-450 > > ovarian2<-subset(ovarian,futime>450) > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > plot(fit1, xlim=c(0,1200), col = 1:2) > abline(v=450) > xylim<-par("usr") > clip(450,xylim[2],xylim[3],xylim[4]) > lines(fit2, col = 3:4,lty=2) > > I can still see that the extra horizontal line on the top. > > Can you or anyone have any suggestion what went wrong? > > Thanks, > > John > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > Hi John, > Perhaps the most direct way would be: > > plot(fit1, col=1:2) > xylim<-par("usr") > clip(4,xylim[2],xylim[3],xylim[4]) > lines(fit2,col=1:2) > > Remember that the new clipping rectangle will persist until you or > something else resets it. > > Jim > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > <[hidden email]> wrote: > > > > Hello, > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > plot(fit1, col=1:2) > > lines(fit2,col=1:2) > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > Can anyone have a strategy to make this kind of plot happen? > > > > Thanks, > > > > John > > > > > ______________________________________________ > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by Jim Lemon-4
Jim,
I tried a few things, I found that clip() works if I just do some regular graphing tasks. But as long as I run lines(fit) with "fit" object is a survfit object, this would reset to default plot region. See the ovarian example below: library(survival) ovarian1<-ovarian ovarian1$fustat[ovarian$futime>450]<-0 ovarian1$futime[ovarian$futime>450]<-450 ovarian2<-subset(ovarian,futime>450) fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) plot(fit1, xlim=c(0,1200), col = 1:2) abline(v=450) xylim<-par("usr") points(-1,-1) clip(450,xylim[2],xylim[3],xylim[4]) abline(h=0.5,col=2) ### YES, clipping() works! lines(fit2, col = 3:4,lty=2) ### clipping does not work! reset to default plot region abline(h=0.4,col=2) ### NO, clipping() does not work! So disappointed with this, otherwise this would be such a simple method to do what I want. Thanks, John On Tuesday, September 29, 2020, 07:58:53 PM PDT, Jim Lemon <[hidden email]> wrote: Hi John, I should have remembered this. For some reason, the clip() function doesn't operate until you have issued a graphics command. Try: points(-1,-1) before calling lines() Jim On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > Hi Jim, > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > ovarian1<-ovarian > ovarian1$fustat[ovarian$futime>450]<-0 > ovarian1$futime[ovarian$futime>450]<-450 > > ovarian2<-subset(ovarian,futime>450) > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > plot(fit1, xlim=c(0,1200), col = 1:2) > abline(v=450) > xylim<-par("usr") > clip(450,xylim[2],xylim[3],xylim[4]) > lines(fit2, col = 3:4,lty=2) > > I can still see that the extra horizontal line on the top. > > Can you or anyone have any suggestion what went wrong? > > Thanks, > > John > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > Hi John, > Perhaps the most direct way would be: > > plot(fit1, col=1:2) > xylim<-par("usr") > clip(4,xylim[2],xylim[3],xylim[4]) > lines(fit2,col=1:2) > > Remember that the new clipping rectangle will persist until you or > something else resets it. > > Jim > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > <[hidden email]> wrote: > > > > Hello, > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > plot(fit1, col=1:2) > > lines(fit2,col=1:2) > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > Can anyone have a strategy to make this kind of plot happen? > > > > Thanks, > > > > John > > > > > ______________________________________________ > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi John,
Hmmm, this works: plot(1:10) xylim<-par("usr") clip(5,xylim[2],xylim[3],xylim[4]) lines(10:1) so I suspect that there is a "lines" method that resets the clipping region out of sight. Fortunately Mark Schwartz provided a way to get your plot so I will give the wall against which I have been banging my head a break. Jim On Wed, Sep 30, 2020 at 1:57 PM array chip <[hidden email]> wrote: > > Jim, > > I tried a few things, I found that clip() works if I just do some regular graphing tasks. But as long as I run lines(fit) with "fit" object is a survfit object, this would reset to default plot region. See the ovarian example below: > > library(survival) > ovarian1<-ovarian > ovarian1$fustat[ovarian$futime>450]<-0 > ovarian1$futime[ovarian$futime>450]<-450 > ovarian2<-subset(ovarian,futime>450) > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > plot(fit1, xlim=c(0,1200), col = 1:2) > abline(v=450) > xylim<-par("usr") > points(-1,-1) > clip(450,xylim[2],xylim[3],xylim[4]) > abline(h=0.5,col=2) ### YES, clipping() works! > > lines(fit2, col = 3:4,lty=2) ### clipping does not work! reset to default plot region > abline(h=0.4,col=2) ### NO, clipping() does not work! > > So disappointed with this, otherwise this would be such a simple method to do what I want. > > Thanks, > > John > > On Tuesday, September 29, 2020, 07:58:53 PM PDT, Jim Lemon <[hidden email]> wrote: > > Hi John, > I should have remembered this. For some reason, the clip() function > doesn't operate until you have issued a graphics command. Try: > > points(-1,-1) > > before calling lines() > > Jim > > On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > > > Hi Jim, > > > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > > > ovarian1<-ovarian > > ovarian1$fustat[ovarian$futime>450]<-0 > > ovarian1$futime[ovarian$futime>450]<-450 > > > > ovarian2<-subset(ovarian,futime>450) > > > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > > > plot(fit1, xlim=c(0,1200), col = 1:2) > > abline(v=450) > > xylim<-par("usr") > > clip(450,xylim[2],xylim[3],xylim[4]) > > lines(fit2, col = 3:4,lty=2) > > > > I can still see that the extra horizontal line on the top. > > > > Can you or anyone have any suggestion what went wrong? > > > > Thanks, > > > > John > > > > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > > > > > > > Hi John, > > Perhaps the most direct way would be: > > > > plot(fit1, col=1:2) > > xylim<-par("usr") > > clip(4,xylim[2],xylim[3],xylim[4]) > > lines(fit2,col=1:2) > > > > Remember that the new clipping rectangle will persist until you or > > something else resets it. > > > > Jim > > > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > > <[hidden email]> wrote: > > > > > > Hello, > > > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > > > plot(fit1, col=1:2) > > > lines(fit2,col=1:2) > > > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > > > Can anyone have a strategy to make this kind of plot happen? > > > > > > Thanks, > > > > > > John > > > > > > > > ______________________________________________ > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Thank you Jim for helping! Yes, I will try Mark's method.
John On Wednesday, September 30, 2020, 01:47:55 AM PDT, Jim Lemon <[hidden email]> wrote: Hi John, Hmmm, this works: plot(1:10) xylim<-par("usr") clip(5,xylim[2],xylim[3],xylim[4]) lines(10:1) so I suspect that there is a "lines" method that resets the clipping region out of sight. Fortunately Mark Schwartz provided a way to get your plot so I will give the wall against which I have been banging my head a break. Jim On Wed, Sep 30, 2020 at 1:57 PM array chip <[hidden email]> wrote: > > Jim, > > I tried a few things, I found that clip() works if I just do some regular graphing tasks. But as long as I run lines(fit) with "fit" object is a survfit object, this would reset to default plot region. See the ovarian example below: > > library(survival) > ovarian1<-ovarian > ovarian1$fustat[ovarian$futime>450]<-0 > ovarian1$futime[ovarian$futime>450]<-450 > ovarian2<-subset(ovarian,futime>450) > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > plot(fit1, xlim=c(0,1200), col = 1:2) > abline(v=450) > xylim<-par("usr") > points(-1,-1) > clip(450,xylim[2],xylim[3],xylim[4]) > abline(h=0.5,col=2) ### YES, clipping() works! > > lines(fit2, col = 3:4,lty=2) ### clipping does not work! reset to default plot region > abline(h=0.4,col=2) ### NO, clipping() does not work! > > So disappointed with this, otherwise this would be such a simple method to do what I want. > > Thanks, > > John > > On Tuesday, September 29, 2020, 07:58:53 PM PDT, Jim Lemon <[hidden email]> wrote: > > Hi John, > I should have remembered this. For some reason, the clip() function > doesn't operate until you have issued a graphics command. Try: > > points(-1,-1) > > before calling lines() > > Jim > > On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > > > Hi Jim, > > > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > > > ovarian1<-ovarian > > ovarian1$fustat[ovarian$futime>450]<-0 > > ovarian1$futime[ovarian$futime>450]<-450 > > > > ovarian2<-subset(ovarian,futime>450) > > > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > > > plot(fit1, xlim=c(0,1200), col = 1:2) > > abline(v=450) > > xylim<-par("usr") > > clip(450,xylim[2],xylim[3],xylim[4]) > > lines(fit2, col = 3:4,lty=2) > > > > I can still see that the extra horizontal line on the top. > > > > Can you or anyone have any suggestion what went wrong? > > > > Thanks, > > > > John > > > > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > > > > > > > Hi John, > > Perhaps the most direct way would be: > > > > plot(fit1, col=1:2) > > xylim<-par("usr") > > clip(4,xylim[2],xylim[3],xylim[4]) > > lines(fit2,col=1:2) > > > > Remember that the new clipping rectangle will persist until you or > > something else resets it. > > > > Jim > > > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > > <[hidden email]> wrote: > > > > > > Hello, > > > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > > > plot(fit1, col=1:2) > > > lines(fit2,col=1:2) > > > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > > > Can anyone have a strategy to make this kind of plot happen? > > > > > > Thanks, > > > > > > John > > > > > > > > ______________________________________________ > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by Jim Lemon-4
Hi Jim,
I found out why clip() does not work with lines(survfit.object)! If you look at code of function survival:::lines.survfit, in th middle of the code: do.clip <- getOption("plot.survfit") if (!is.null(xx <- do.clip$plotclip)) clip(xx[1], xx[2], xx[3], xx[4]) This will reset the clipping to the defualt plot region! So I just comment out the last 2 lines of the above 3 lines, and created a customized lines2 function. Now it works! It's fun to learn clip(). Thanks, John On Wednesday, September 30, 2020, 01:47:55 AM PDT, Jim Lemon <[hidden email]> wrote: Hi John, Hmmm, this works: plot(1:10) xylim<-par("usr") clip(5,xylim[2],xylim[3],xylim[4]) lines(10:1) so I suspect that there is a "lines" method that resets the clipping region out of sight. Fortunately Mark Schwartz provided a way to get your plot so I will give the wall against which I have been banging my head a break. Jim On Wed, Sep 30, 2020 at 1:57 PM array chip <[hidden email]> wrote: > > Jim, > > I tried a few things, I found that clip() works if I just do some regular graphing tasks. But as long as I run lines(fit) with "fit" object is a survfit object, this would reset to default plot region. See the ovarian example below: > > library(survival) > ovarian1<-ovarian > ovarian1$fustat[ovarian$futime>450]<-0 > ovarian1$futime[ovarian$futime>450]<-450 > ovarian2<-subset(ovarian,futime>450) > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > plot(fit1, xlim=c(0,1200), col = 1:2) > abline(v=450) > xylim<-par("usr") > points(-1,-1) > clip(450,xylim[2],xylim[3],xylim[4]) > abline(h=0.5,col=2) ### YES, clipping() works! > > lines(fit2, col = 3:4,lty=2) ### clipping does not work! reset to default plot region > abline(h=0.4,col=2) ### NO, clipping() does not work! > > So disappointed with this, otherwise this would be such a simple method to do what I want. > > Thanks, > > John > > On Tuesday, September 29, 2020, 07:58:53 PM PDT, Jim Lemon <[hidden email]> wrote: > > Hi John, > I should have remembered this. For some reason, the clip() function > doesn't operate until you have issued a graphics command. Try: > > points(-1,-1) > > before calling lines() > > Jim > > On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > > > Hi Jim, > > > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > > > ovarian1<-ovarian > > ovarian1$fustat[ovarian$futime>450]<-0 > > ovarian1$futime[ovarian$futime>450]<-450 > > > > ovarian2<-subset(ovarian,futime>450) > > > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > > > plot(fit1, xlim=c(0,1200), col = 1:2) > > abline(v=450) > > xylim<-par("usr") > > clip(450,xylim[2],xylim[3],xylim[4]) > > lines(fit2, col = 3:4,lty=2) > > > > I can still see that the extra horizontal line on the top. > > > > Can you or anyone have any suggestion what went wrong? > > > > Thanks, > > > > John > > > > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > > > > > > > Hi John, > > Perhaps the most direct way would be: > > > > plot(fit1, col=1:2) > > xylim<-par("usr") > > clip(4,xylim[2],xylim[3],xylim[4]) > > lines(fit2,col=1:2) > > > > Remember that the new clipping rectangle will persist until you or > > something else resets it. > > > > Jim > > > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > > <[hidden email]> wrote: > > > > > > Hello, > > > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > > > plot(fit1, col=1:2) > > > lines(fit2,col=1:2) > > > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > > > Can anyone have a strategy to make this kind of plot happen? > > > > > > Thanks, > > > > > > John > > > > > > > > ______________________________________________ > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi John,
Brilliant solution and the best sort - when you finally solve your problem by yourself. Jim On Thu, Oct 1, 2020 at 2:52 AM array chip <[hidden email]> wrote: > > Hi Jim, > > I found out why clip() does not work with lines(survfit.object)! > > If you look at code of function survival:::lines.survfit, in th middle of the code: > > do.clip <- getOption("plot.survfit") > if (!is.null(xx <- do.clip$plotclip)) > clip(xx[1], xx[2], xx[3], xx[4]) > > This will reset the clipping to the defualt plot region! > > So I just comment out the last 2 lines of the above 3 lines, and created a customized lines2 function. Now it works! > > It's fun to learn clip(). > > Thanks, > > John > > > On Wednesday, September 30, 2020, 01:47:55 AM PDT, Jim Lemon <[hidden email]> wrote: > > > Hi John, > Hmmm, this works: > > plot(1:10) > xylim<-par("usr") > clip(5,xylim[2],xylim[3],xylim[4]) > lines(10:1) > > so I suspect that there is a "lines" method that resets the clipping > region out of sight. Fortunately Mark Schwartz provided a way to get > your plot so I will give the wall against which I have been banging my > head a break. > > Jim > > On Wed, Sep 30, 2020 at 1:57 PM array chip <[hidden email]> wrote: > > > > Jim, > > > > I tried a few things, I found that clip() works if I just do some regular graphing tasks. But as long as I run lines(fit) with "fit" object is a survfit object, this would reset to default plot region. See the ovarian example below: > > > > library(survival) > > ovarian1<-ovarian > > ovarian1$fustat[ovarian$futime>450]<-0 > > ovarian1$futime[ovarian$futime>450]<-450 > > ovarian2<-subset(ovarian,futime>450) > > > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > > > plot(fit1, xlim=c(0,1200), col = 1:2) > > abline(v=450) > > xylim<-par("usr") > > points(-1,-1) > > clip(450,xylim[2],xylim[3],xylim[4]) > > abline(h=0.5,col=2) ### YES, clipping() works! > > > > lines(fit2, col = 3:4,lty=2) ### clipping does not work! reset to default plot region > > abline(h=0.4,col=2) ### NO, clipping() does not work! > > > > So disappointed with this, otherwise this would be such a simple method to do what I want. > > > > Thanks, > > > > John > > > > On Tuesday, September 29, 2020, 07:58:53 PM PDT, Jim Lemon <[hidden email]> wrote: > > > > Hi John, > > I should have remembered this. For some reason, the clip() function > > doesn't operate until you have issued a graphics command. Try: > > > > points(-1,-1) > > > > before calling lines() > > > > Jim > > > > On Wed, Sep 30, 2020 at 12:26 PM array chip <[hidden email]> wrote: > > > > > > Hi Jim, > > > > > > I tried the clip() function below, surprisingly it did not work! I read the R help file and feel your script should work. To have a workable example, I used the ovarian dataset in the survival package as an example: > > > > > > ovarian1<-ovarian > > > ovarian1$fustat[ovarian$futime>450]<-0 > > > ovarian1$futime[ovarian$futime>450]<-450 > > > > > > ovarian2<-subset(ovarian,futime>450) > > > > > > fit1 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian1) > > > fit2 <- survfit(Surv(futime, fustat) ~ rx, data = ovarian2) > > > > > > plot(fit1, xlim=c(0,1200), col = 1:2) > > > abline(v=450) > > > xylim<-par("usr") > > > clip(450,xylim[2],xylim[3],xylim[4]) > > > lines(fit2, col = 3:4,lty=2) > > > > > > I can still see that the extra horizontal line on the top. > > > > > > Can you or anyone have any suggestion what went wrong? > > > > > > Thanks, > > > > > > John > > > > > > > > > On Tuesday, September 29, 2020, 01:35:48 AM PDT, Jim Lemon <[hidden email]> wrote: > > > > > > > > > > > > > > > > > > Hi John, > > > Perhaps the most direct way would be: > > > > > > plot(fit1, col=1:2) > > > xylim<-par("usr") > > > clip(4,xylim[2],xylim[3],xylim[4]) > > > lines(fit2,col=1:2) > > > > > > Remember that the new clipping rectangle will persist until you or > > > something else resets it. > > > > > > Jim > > > > > > On Tue, Sep 29, 2020 at 10:34 AM array chip via R-help > > > <[hidden email]> wrote: > > > > > > > > Hello, > > > > > > > > Can anyone suggest a simple way to generate a Kaplan-Meier plot with 2 survfit objects, just like this one: > > > > > > > > https://drive.google.com/file/d/1fEcpdIdE2xYtA6LBQN9ck3JkL6-goabX/view?usp=sharing > > > > > > > > Suppose I have 2 survfit objects: fit1 is for the curve on the left (survtime has been truncated to the cutoff line: year 5), fit2 is for the curve on the right (minimum survival time is at the cutoff line: year 5), but if I do the following: > > > > > > > > plot(fit1, col=1:2) > > > > lines(fit2,col=1:2) > > > > > > > > Then I will have an horizontal line on the top that connect from 0 to 4 years, which I do not want that to be drawn (see blue arrow below): > > > > > > > > https://drive.google.com/file/d/178mQGlhnaOg9PA-oE-W_W5CtrGD03ljH/view?usp=sharing > > > > > > > > Can anyone have a strategy to make this kind of plot happen? > > > > > > > > Thanks, > > > > > > > > John > > > > > > > > > > > ______________________________________________ > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > > > and provide commented, minimal, self-contained, reproducible code. > > > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Free forum by Nabble | Edit this page |