[R] add lines() to 1st plot in layout() after calling 2nd plot()?

baptiste auguie baptiste.auguie at googlemail.com
Sun Oct 4 11:32:39 CEST 2009


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.promberger at kcl.ac.uk>:
> Thanks for the quick reply. However ...
>
> David Winsemius <dwinsemius at 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 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.
>
> --
> Marianne Promberger PhD, King's College London
> http://promberger.info
> R version 2.9.2 (2009-08-24)
> Ubuntu 9.04
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list