I am doing calculations in a loop and then plotting the results by adding a point to each of 2 charts at the end of the loop. Its very informative as you can see the progression through time. My problem is, if I have 2 plots, I don't know how to get the focus back to the first plot. layout(matrix(c(1,2))) plot(iris[,1],col="red",) #plot1 plot(iris[,3],col="blue") #plot2 #goes on plot2 lines(iris[,2],col="pink") #how do I put this line on plot 1 lines(iris[,4],col="black") I tried the method below but when you switch the focus back to screen 1 the line gets drawn not where I expect split.screen(c(2,1)) screen(1) # prepare screen 1 for output plot(iris[,1],col="red",) #plot1 screen(2) # prepare screen 2 for output plot(iris[,3],col="blue") #plot2 screen(1) lines(iris[,2],col="pink",lwd=8) screen(2) lines(iris[,4],col="green",lwd=8) Any pointers please as to what I need to do? -- View this message in context: http://r.789695.n4.nabble.com/plot-focus-tp2272699p2272699.html Sent from the R help mailing list archive at Nabble.com.
You could just open two devices. See ?dev.cur for an example. -Peter Ehlers On 2010-06-30 4:26, pdb wrote:> > I am doing calculations in a loop and then plotting the results by adding a > point to each of 2 charts at the end of the loop. Its very informative as > you can see the progression through time. > > My problem is, if I have 2 plots, I don't know how to get the focus back to > the first plot. > > layout(matrix(c(1,2))) > > plot(iris[,1],col="red",) #plot1 > plot(iris[,3],col="blue") #plot2 > > #goes on plot2 > lines(iris[,2],col="pink") > > #how do I put this line on plot 1 > lines(iris[,4],col="black") > > > I tried the method below but when you switch the focus back to screen 1 the > line gets drawn not where I expect > > split.screen(c(2,1)) > screen(1) # prepare screen 1 for output > plot(iris[,1],col="red",) #plot1 > screen(2) # prepare screen 2 for output > plot(iris[,3],col="blue") #plot2 > > screen(1) > lines(iris[,2],col="pink",lwd=8) > > screen(2) > lines(iris[,4],col="green",lwd=8) > > Any pointers please as to what I need to do? >
Good question pdb, I would suggest you to use: par(bg = "white") In the beginning of the code, But it doesn't solve the general problem of how to get the "lines" to be properly aligned. I am curious for the answer from betteR people. Best, Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Wed, Jun 30, 2010 at 1:26 PM, pdb <philb@philbrierley.com> wrote:> > I am doing calculations in a loop and then plotting the results by adding a > point to each of 2 charts at the end of the loop. Its very informative as > you can see the progression through time. > > My problem is, if I have 2 plots, I don't know how to get the focus back to > the first plot. > > layout(matrix(c(1,2))) > > plot(iris[,1],col="red",) #plot1 > plot(iris[,3],col="blue") #plot2 > > #goes on plot2 > lines(iris[,2],col="pink") > > #how do I put this line on plot 1 > lines(iris[,4],col="black") > > > I tried the method below but when you switch the focus back to screen 1 the > line gets drawn not where I expect > > split.screen(c(2,1)) > screen(1) # prepare screen 1 for output > plot(iris[,1],col="red",) #plot1 > screen(2) # prepare screen 2 for output > plot(iris[,3],col="blue") #plot2 > > screen(1) > lines(iris[,2],col="pink",lwd=8) > > screen(2) > lines(iris[,4],col="green",lwd=8) > > Any pointers please as to what I need to do? > > -- > View this message in context: > http://r.789695.n4.nabble.com/plot-focus-tp2272699p2272699.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]
I would generally write a function to create the entire first plot, then the entire 2nd plot based on data or other arguments passed in, then in your loop or whatever call the function with the different steps. This will recreate the plots from scratch at each step rather than adding to the existing plots, but often works simpler. If you really want to add to the plots, here is one way: library(TeachingDemos) plot.new() p1 <- subplot( plot(iris[,1], col='red', ylim=range(iris[,1:2])), x=c(0,1), y=c(0.6,1) ) p2 <- subplot( plot(iris[,3], col='blue', ylim=range(iris[,3:4])), x=c(0,1), y=c(0,0.4) ) par(p1) lines(iris[,2],col="pink",lwd=8) par(p2) lines(iris[,4],col="green",lwd=8) hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of pdb > Sent: Wednesday, June 30, 2010 4:27 AM > To: r-help at r-project.org > Subject: [R] plot focus > > > I am doing calculations in a loop and then plotting the results by > adding a > point to each of 2 charts at the end of the loop. Its very informative > as > you can see the progression through time. > > My problem is, if I have 2 plots, I don't know how to get the focus > back to > the first plot. > > layout(matrix(c(1,2))) > > plot(iris[,1],col="red",) #plot1 > plot(iris[,3],col="blue") #plot2 > > #goes on plot2 > lines(iris[,2],col="pink") > > #how do I put this line on plot 1 > lines(iris[,4],col="black") > > > I tried the method below but when you switch the focus back to screen 1 > the > line gets drawn not where I expect > > split.screen(c(2,1)) > screen(1) # prepare screen 1 for output > plot(iris[,1],col="red",) #plot1 > screen(2) # prepare screen 2 for output > plot(iris[,3],col="blue") #plot2 > > screen(1) > lines(iris[,2],col="pink",lwd=8) > > screen(2) > lines(iris[,4],col="green",lwd=8) > > Any pointers please as to what I need to do? > > -- > View this message in context: http://r.789695.n4.nabble.com/plot-focus- > tp2272699p2272699.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Try this: par(mfg = c(1, 1)) lines(iris[,4],col="black") On Wed, Jun 30, 2010 at 7:26 AM, pdb <philb@philbrierley.com> wrote:> > I am doing calculations in a loop and then plotting the results by adding a > point to each of 2 charts at the end of the loop. Its very informative as > you can see the progression through time. > > My problem is, if I have 2 plots, I don't know how to get the focus back to > the first plot. > > layout(matrix(c(1,2))) > > plot(iris[,1],col="red",) #plot1 > plot(iris[,3],col="blue") #plot2 > > #goes on plot2 > lines(iris[,2],col="pink") > > #how do I put this line on plot 1 > lines(iris[,4],col="black") > > > I tried the method below but when you switch the focus back to screen 1 the > line gets drawn not where I expect > > split.screen(c(2,1)) > screen(1) # prepare screen 1 for output > plot(iris[,1],col="red",) #plot1 > screen(2) # prepare screen 2 for output > plot(iris[,3],col="blue") #plot2 > > screen(1) > lines(iris[,2],col="pink",lwd=8) > > screen(2) > lines(iris[,4],col="green",lwd=8) > > Any pointers please as to what I need to do? > > -- > View this message in context: > http://r.789695.n4.nabble.com/plot-focus-tp2272699p2272699.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Thanks Henrique, that appeared to work, but now I have another issue. If I add a ylim to the plot then when I plot another line it gets plotted on the wrong scale. #this works as expected plot(iris[,1],col="red",ylim=c(-10,10)) #plot1 lines(iris[,4],col="black") #this does not par(mfrow=c(2,1)) plot(iris[,1],col="red",ylim=c(-10,10)) #plot1 plot(iris[,3],col="blue") #plot2 #goes on plot2 par(mfg = c(2, 1)) lines(iris[,2],col="pink") #goes on plot 1 par(mfg = c(1, 1)) lines(iris[,4],col="black") -- View this message in context: http://r.789695.n4.nabble.com/plot-focus-tp2272699p2274541.html Sent from the R help mailing list archive at Nabble.com.