Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
Since you are using LaTeX, you might consider creating the plots all in 1 step, but use the tikzdevice, this provides LaTeX (pgf) commands to create the plot that you can then insert LaTeX commands to built up the plot bit by bit. You could also look at using the subplot function in the TeachingDemos package, start with a blank plot without margins, then use subplot in place of layout to position your graphs, the examples in the help file for subplot show how to add to the subplot after creating it. Another option is to create a function that creates each whole plot, but with an argument and some if statements that will skip certain steps, then just do a for loop calling the functions to build the plots in pieces. This means replotting from scratch each step, but save these as graphics files and it is not overly complex. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Marianne Promberger Sent: Saturday, October 03, 2009 1:15 PM To: r-help Subject: [R] add lines() to 1st plot in layout() after calling 2nd plot()? Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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. __ 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.
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
This only works if all the plots are the same size and the defaults are used for the margins. Try it with different sized figure regions in layout, the added lines don't match at the end. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of baptiste auguie Sent: Sunday, October 04, 2009 3:33 AM To: r-help Subject: Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()? Hi, Try this, dev.new() layout(matrix(1:4,2, by=T)) plot(1:10,main=top left plot) plot(1:10,main=top right plot) plot(1:10,main=bottom left plot) plot(1:10,main=bottom right plot) for (ii in 1:2){ for (jj in 1:2){ par(mfg=c(ii,jj)) text(5,2, lab=paste(plot #:,ii,,,jj,sep=)) } } par(mfg=c(1,1)) lines(c(3:7,7:3),col=red) HTH, baptiste 2009/10/4 Marianne Promberger marianne.promber...@kcl.ac.uk: Thanks for the quick reply. However ... David Winsemius dwinsem...@comcast.net 03-Oct-09 20:50: MP layout(matrix(c(1,2),1)) MP plot(1:10,main=left plot) MP plot(10:1,main=right plot) MP lines(c(3:7,7:3),col=red) MP MP but of course now lines() gets added to the right plot. I MP MP Is there any way to make the lines() go to the fist plot (left MP plot)? If you look at layout's help page there appears to be a worked example of an even more complex task. The answer appears to be assingning numbers to regions and then inserting par(mar= with an appropriately constructed destination arguments prior to each added piece. Sorry, but I fail to find the solution in the page returned by ?layout, assuming that's what you mean. Yes, the numbers in the matrix given to layout() give the order of where plots will be put, so layout(matrix(c(2,1),1)) then plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) puts left plot on the right hand side and right plot on the left. But the lines() still go to the right plot plot (now on the left hand side) which gets called last. The par(mar ... of the scatterplot with marginal histograms example just set the margins of the histogram plots, then they get plotted to the region with the next number given in the layout() matrix. Maybe I'm missing something. Thanks, Marianne -- David On Oct 3, 2009, at 3:15 PM, Marianne Promberger wrote: Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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. __ 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. __ 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.
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
Interesting, I hadn't tried this but it probably explains why navigation to different regions of a layout is neither documented nor advisable. Yet another alternative is to use Grid graphics. In particular, 1- lattice or ggplot2 provide ways to arrange several plots in a rectangular layout, with several options to add output to a specific panel (but they do require quite a different approach to the creation of plots) 2- for simple enough plots (or for brave users) you could also use low-level grid commands, and navigate to different viewports arbitrarily placed on a page. 3- the gridBase package provides a way to combine (with some limitations) the power of grid layouts and the output produced with base graphics. Best, baptiste 2009/10/12 Greg Snow greg.s...@imail.org: This only works if all the plots are the same size and the defaults are used for the margins. Try it with different sized figure regions in layout, the added lines don't match at the end. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of baptiste auguie Sent: Sunday, October 04, 2009 3:33 AM To: r-help Subject: Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()? Hi, Try this, dev.new() layout(matrix(1:4,2, by=T)) plot(1:10,main=top left plot) plot(1:10,main=top right plot) plot(1:10,main=bottom left plot) plot(1:10,main=bottom right plot) for (ii in 1:2){ for (jj in 1:2){ par(mfg=c(ii,jj)) text(5,2, lab=paste(plot #:,ii,,,jj,sep=)) } } par(mfg=c(1,1)) lines(c(3:7,7:3),col=red) HTH, baptiste 2009/10/4 Marianne Promberger marianne.promber...@kcl.ac.uk: Thanks for the quick reply. However ... David Winsemius dwinsem...@comcast.net 03-Oct-09 20:50: MP layout(matrix(c(1,2),1)) MP plot(1:10,main=left plot) MP plot(10:1,main=right plot) MP lines(c(3:7,7:3),col=red) MP MP but of course now lines() gets added to the right plot. I MP MP Is there any way to make the lines() go to the fist plot (left MP plot)? If you look at layout's help page there appears to be a worked example of an even more complex task. The answer appears to be assingning numbers to regions and then inserting par(mar= with an appropriately constructed destination arguments prior to each added piece. Sorry, but I fail to find the solution in the page returned by ?layout, assuming that's what you mean. Yes, the numbers in the matrix given to layout() give the order of where plots will be put, so layout(matrix(c(2,1),1)) then plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) puts left plot on the right hand side and right plot on the left. But the lines() still go to the right plot plot (now on the left hand side) which gets called last. The par(mar ... of the scatterplot with marginal histograms example just set the margins of the histogram plots, then they get plotted to the region with the next number given in the layout() matrix. Maybe I'm missing something. Thanks, Marianne -- David On Oct 3, 2009, at 3:15 PM, Marianne Promberger wrote: Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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. __ R-help@r
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
Thanks for the quick reply. However ... David Winsemius dwinsem...@comcast.net 03-Oct-09 20:50: MP layout(matrix(c(1,2),1)) MP plot(1:10,main=left plot) MP plot(10:1,main=right plot) MP lines(c(3:7,7:3),col=red) MP MP but of course now lines() gets added to the right plot. I MP MP Is there any way to make the lines() go to the fist plot (left MP plot)? If you look at layout's help page there appears to be a worked example of an even more complex task. The answer appears to be assingning numbers to regions and then inserting par(mar= with an appropriately constructed destination arguments prior to each added piece. Sorry, but I fail to find the solution in the page returned by ?layout, assuming that's what you mean. Yes, the numbers in the matrix given to layout() give the order of where plots will be put, so layout(matrix(c(2,1),1)) then plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) puts left plot on the right hand side and right plot on the left. But the lines() still go to the right plot plot (now on the left hand side) which gets called last. The par(mar ... of the scatterplot with marginal histograms example just set the margins of the histogram plots, then they get plotted to the region with the next number given in the layout() matrix. Maybe I'm missing something. Thanks, Marianne -- David On Oct 3, 2009, at 3:15 PM, Marianne Promberger wrote: Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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.
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
Hi, Try this, dev.new() layout(matrix(1:4,2, by=T)) plot(1:10,main=top left plot) plot(1:10,main=top right plot) plot(1:10,main=bottom left plot) plot(1:10,main=bottom right plot) for (ii in 1:2){ for (jj in 1:2){ par(mfg=c(ii,jj)) text(5,2, lab=paste(plot #:,ii,,,jj,sep=)) } } par(mfg=c(1,1)) lines(c(3:7,7:3),col=red) HTH, baptiste 2009/10/4 Marianne Promberger marianne.promber...@kcl.ac.uk: Thanks for the quick reply. However ... David Winsemius dwinsem...@comcast.net 03-Oct-09 20:50: MP layout(matrix(c(1,2),1)) MP plot(1:10,main=left plot) MP plot(10:1,main=right plot) MP lines(c(3:7,7:3),col=red) MP MP but of course now lines() gets added to the right plot. I MP MP Is there any way to make the lines() go to the fist plot (left MP plot)? If you look at layout's help page there appears to be a worked example of an even more complex task. The answer appears to be assingning numbers to regions and then inserting par(mar= with an appropriately constructed destination arguments prior to each added piece. Sorry, but I fail to find the solution in the page returned by ?layout, assuming that's what you mean. Yes, the numbers in the matrix given to layout() give the order of where plots will be put, so layout(matrix(c(2,1),1)) then plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) puts left plot on the right hand side and right plot on the left. But the lines() still go to the right plot plot (now on the left hand side) which gets called last. The par(mar ... of the scatterplot with marginal histograms example just set the margins of the histogram plots, then they get plotted to the region with the next number given in the layout() matrix. Maybe I'm missing something. Thanks, Marianne -- David On Oct 3, 2009, at 3:15 PM, Marianne Promberger wrote: Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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. __ 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.
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
After some checking, I think the documentation is at best misleading on this particular query. Nowhere could I find mention that par(mfg) can work with layout. Instead, I found a warning that suggests incompatibility (since mpg is explicitly tied to mfrow in ?par and layout is said incompatible with par(mfrow)). Relevant bits from ?par: mfcol, mfrow Consider the alternatives, layout and split.screen. mfg A numerical vector of the form c(i, j) where i and j indicate which figure in an array of figures is to be drawn next (if setting) or is being drawn (if enquiring). The array must already have been set by mfcol or mfrow. From ?layout, Warnings These functions are totally incompatible with the other mechanisms for arranging plots on a device: par(mfrow), par(mfcol) and split.screen. See Also par with arguments mfrow, mfcol, or mfg. I suggest the following changes, in ?layout, section Details: The user can navigate to a given cell using the the mpg parameter (see ?par). in ?par, parameter mpg: The array must already have been set by either mfcol, mfrow, or layout. It may be that I missed an important incompatibility in complicated layouts, but it seems to work at least for simple ones. Best regards, baptiste 2009/10/4 baptiste auguie baptiste.aug...@googlemail.com: Hi, Try this, dev.new() layout(matrix(1:4,2, by=T)) plot(1:10,main=top left plot) plot(1:10,main=top right plot) plot(1:10,main=bottom left plot) plot(1:10,main=bottom right plot) for (ii in 1:2){ for (jj in 1:2){ par(mfg=c(ii,jj)) text(5,2, lab=paste(plot #:,ii,,,jj,sep=)) } } par(mfg=c(1,1)) lines(c(3:7,7:3),col=red) HTH, baptiste 2009/10/4 Marianne Promberger marianne.promber...@kcl.ac.uk: Thanks for the quick reply. However ... David Winsemius dwinsem...@comcast.net 03-Oct-09 20:50: MP layout(matrix(c(1,2),1)) MP plot(1:10,main=left plot) MP plot(10:1,main=right plot) MP lines(c(3:7,7:3),col=red) MP MP but of course now lines() gets added to the right plot. I MP MP Is there any way to make the lines() go to the fist plot (left MP plot)? If you look at layout's help page there appears to be a worked example of an even more complex task. The answer appears to be assingning numbers to regions and then inserting par(mar= with an appropriately constructed destination arguments prior to each added piece. Sorry, but I fail to find the solution in the page returned by ?layout, assuming that's what you mean. Yes, the numbers in the matrix given to layout() give the order of where plots will be put, so layout(matrix(c(2,1),1)) then plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) puts left plot on the right hand side and right plot on the left. But the lines() still go to the right plot plot (now on the left hand side) which gets called last. The par(mar ... of the scatterplot with marginal histograms example just set the margins of the histogram plots, then they get plotted to the region with the next number given in the layout() matrix. Maybe I'm missing something. Thanks, Marianne -- David On Oct 3, 2009, at 3:15 PM, Marianne Promberger wrote: Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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. __ 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,
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
baptiste auguie 04-Oct-09 10:32: dev.new() layout(matrix(1:4,2, by=T)) plot(1:10,main=top left plot) plot(1:10,main=top right plot) plot(1:10,main=bottom left plot) plot(1:10,main=bottom right plot) [...] par(mfg=c(1,1)) lines(c(3:7,7:3),col=red) Brilliant! Thanks! baptiste auguie 04-Oct-09 10:50: After some checking, I think the documentation is at best misleading on this particular query. Nowhere could I find mention that par(mfg) can work with layout. Instead, I found a warning that suggests incompatibility (since mpg is explicitly tied to mfrow in ?par and layout is said incompatible with par(mfrow)). Indeed. I did look around ?par, but due to this warning thought anything relating to mfrow() and mfcol() would not work anyway. Thanks again, Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.9.2 (2009-08-24) Ubuntu 9.04 __ 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.
Re: [R] add lines() to 1st plot in layout() after calling 2nd plot()?
If you look at layout's help page there appears to be a worked example of an even more complex task. The answer appears to be assingning numbers to regions and then inserting par(mar= with an appropriately constructed destination arguments prior to each added piece. -- David On Oct 3, 2009, at 3:15 PM, Marianne Promberger wrote: Dear R users, I create a graphic with two plots side by side using layout(), like this: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) lines(c(3:7,7:3),col=red) plot(10:1,main=right plot) The lines() obivously get added to the left plot plot. Now, I'm trying to write a function that builds up a plot bit by bit to then include it in a LaTeX presentation with overlays. I'm using dev.copy(), and it would make my life much easier (because in fact I call all sorts of additional axis() etc after plot) if I could call the above commands in this order: layout(matrix(c(1,2),1)) plot(1:10,main=left plot) plot(10:1,main=right plot) lines(c(3:7,7:3),col=red) but of course now lines() gets added to the right plot. I Is there any way to make the lines() go to the fist plot (left plot)? Marianne David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.