Re: [R] placing rectangle behind plot
Hi Gabor Grothendieck wrote: Thanks. That's helpful. I would be interested in the case where 1. one does not have a variable latticeplot, as per your example, but just has the output of xyplot(x ~ x | gl(2,1), layout = 1:2) sitting on the screen, having been printed by a prior function. We can assume that no other graphics have been issued since then. Can one still create a grey rectangle behind the lower panel? xyplot(x ~ x | gl(2,1), layout = 1:2) latticeplot - grid.grab() # Continue as for grid.grabExpr() example ... 2. In fact, ideally what I would like is to create a function, put.in.bg, say, that works something like this: xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) put.in.bg(grid.rect(w = 0.5)) trellis.unfocus() or maybe xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus.bg(panel, 1, 1) grid.rect(w = 0.5) trellis.unfocus() That allows one to add objects to a lattice panel behind the objects that are already there. This would also be helpful for adding grid lines afterwards or other lines, rectangles, etc. I could imagine something like ... xyplot(x ~ x | gl(2,1), layout = 1:2) put.in.bg(panel, 1, 1, rectGrob(w = 0.5)) ... where you just wrap the approach I described (using grid.grab() to capture the existing plot, then modifying the resulting grob), but such a function would obviously not work well when called after something other than just a trellis plot. Paul On 7/30/06, Paul Murrell [EMAIL PROTECTED] wrote: Hi Gabor Grothendieck wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? Correct. At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() The user-interface is a little rough, but this can be done by accessing the underlying grid objects. Here's an example, with explanatory bits interspersed ... # grab the lattice plot as a grid gTree # There are warnings, but they are ignorable latticeplot - grid.grabExpr(print(xyplot(x ~ x | gl(2,1), layout = 1:2))) # Demonstrate that the gTree faithfully replicates the # original lattice plot (not necessary, just to to what's going on) grid.newpage() grid.draw(latticeplot) # Explore the gTree (just to to show what's going on) # Better user-interface would be nice here ... childNames(latticeplot) # Identify which children are which # (appropriate grob names would be nice here) lapply(latticeplot$children, class) # Identify where each child is drawn latticeplot$childrenvp lapply(latticeplot$children, [[, vp) # Add a rect (starts off on top of everything else) # NOTE that rect has to have correct vpPath plotwithrect - addGrob(latticeplot, rectGrob(w = .5, gp = gpar(fill = light grey), vp=vpPath(plot1.toplevel.vp, plot1.panel.1.1.vp))) # Check this draws what we expect (just to show what's going on) grid.newpage() grid.draw(plotwithrect) # Reorder children to put rect at back # Appropriate user-interface would be nice here ... nc - length(plotwithrect$childrenOrder) plotwithrect$childrenOrder - plotwithrect$childrenOrder[c(nc, 1:(nc - 1))] # Final result grid.newpage() grid.draw(plotwithrect) Paul -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch 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] placing rectangle behind plot
One thing you could try (probably as last resort, if someone comes up with a better idea, use that) is to plot to an xfig device, then use xfig (or jfig) to move the rectangle to the back, then convert it to whatever final graphics format you want. -Original Message- From: [EMAIL PROTECTED] on behalf of Gabor Grothendieck Sent: Sat 7/29/2006 3:20 PM To: RHelp Subject: [R] placing rectangle behind plot I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() __ R-help@stat.math.ethz.ch 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]] __ R-help@stat.math.ethz.ch 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] placing rectangle behind plot
Hi Gabor Grothendieck wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? Correct. At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() The user-interface is a little rough, but this can be done by accessing the underlying grid objects. Here's an example, with explanatory bits interspersed ... # grab the lattice plot as a grid gTree # There are warnings, but they are ignorable latticeplot - grid.grabExpr(print(xyplot(x ~ x | gl(2,1), layout = 1:2))) # Demonstrate that the gTree faithfully replicates the # original lattice plot (not necessary, just to to what's going on) grid.newpage() grid.draw(latticeplot) # Explore the gTree (just to to show what's going on) # Better user-interface would be nice here ... childNames(latticeplot) # Identify which children are which # (appropriate grob names would be nice here) lapply(latticeplot$children, class) # Identify where each child is drawn latticeplot$childrenvp lapply(latticeplot$children, [[, vp) # Add a rect (starts off on top of everything else) # NOTE that rect has to have correct vpPath plotwithrect - addGrob(latticeplot, rectGrob(w = .5, gp = gpar(fill = light grey), vp=vpPath(plot1.toplevel.vp, plot1.panel.1.1.vp))) # Check this draws what we expect (just to show what's going on) grid.newpage() grid.draw(plotwithrect) # Reorder children to put rect at back # Appropriate user-interface would be nice here ... nc - length(plotwithrect$childrenOrder) plotwithrect$childrenOrder - plotwithrect$childrenOrder[c(nc, 1:(nc - 1))] # Final result grid.newpage() grid.draw(plotwithrect) Paul -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch 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] placing rectangle behind plot
Thanks. That's helpful. I would be interested in the case where 1. one does not have a variable latticeplot, as per your example, but just has the output of xyplot(x ~ x | gl(2,1), layout = 1:2) sitting on the screen, having been printed by a prior function. We can assume that no other graphics have been issued since then. Can one still create a grey rectangle behind the lower panel? 2. In fact, ideally what I would like is to create a function, put.in.bg, say, that works something like this: xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) put.in.bg(grid.rect(w = 0.5)) trellis.unfocus() or maybe xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus.bg(panel, 1, 1) grid.rect(w = 0.5) trellis.unfocus() That allows one to add objects to a lattice panel behind the objects that are already there. This would also be helpful for adding grid lines afterwards or other lines, rectangles, etc. On 7/30/06, Paul Murrell [EMAIL PROTECTED] wrote: Hi Gabor Grothendieck wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? Correct. At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() The user-interface is a little rough, but this can be done by accessing the underlying grid objects. Here's an example, with explanatory bits interspersed ... # grab the lattice plot as a grid gTree # There are warnings, but they are ignorable latticeplot - grid.grabExpr(print(xyplot(x ~ x | gl(2,1), layout = 1:2))) # Demonstrate that the gTree faithfully replicates the # original lattice plot (not necessary, just to to what's going on) grid.newpage() grid.draw(latticeplot) # Explore the gTree (just to to show what's going on) # Better user-interface would be nice here ... childNames(latticeplot) # Identify which children are which # (appropriate grob names would be nice here) lapply(latticeplot$children, class) # Identify where each child is drawn latticeplot$childrenvp lapply(latticeplot$children, [[, vp) # Add a rect (starts off on top of everything else) # NOTE that rect has to have correct vpPath plotwithrect - addGrob(latticeplot, rectGrob(w = .5, gp = gpar(fill = light grey), vp=vpPath(plot1.toplevel.vp, plot1.panel.1.1.vp))) # Check this draws what we expect (just to show what's going on) grid.newpage() grid.draw(plotwithrect) # Reorder children to put rect at back # Appropriate user-interface would be nice here ... nc - length(plotwithrect$childrenOrder) plotwithrect$childrenOrder - plotwithrect$childrenOrder[c(nc, 1:(nc - 1))] # Final result grid.newpage() grid.draw(plotwithrect) Paul -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch 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] placing rectangle behind plot
Just to answer my own question I just discovered trellis.panelArgs() and that can be used to give the following solution: library(lattice) library(grid) x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = 0.5, gp = gpar(fill = light grey)) # re-plot panel over rectangle do.call(panel.xyplot, trellis.panelArgs()) trellis.unfocus() nevertheless, as a point of general interest I would still be interested to know what a general grid-based solution might be. On 7/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Thanks. That's helpful. I would be interested in the case where 1. one does not have a variable latticeplot, as per your example, but just has the output of xyplot(x ~ x | gl(2,1), layout = 1:2) sitting on the screen, having been printed by a prior function. We can assume that no other graphics have been issued since then. Can one still create a grey rectangle behind the lower panel? 2. In fact, ideally what I would like is to create a function, put.in.bg, say, that works something like this: xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) put.in.bg(grid.rect(w = 0.5)) trellis.unfocus() or maybe xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus.bg(panel, 1, 1) grid.rect(w = 0.5) trellis.unfocus() That allows one to add objects to a lattice panel behind the objects that are already there. This would also be helpful for adding grid lines afterwards or other lines, rectangles, etc. On 7/30/06, Paul Murrell [EMAIL PROTECTED] wrote: Hi Gabor Grothendieck wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? Correct. At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? library(lattice) library(grid) trellis.unfocus() x - 1:10 xyplot(x ~ x | gl(2,1), layout = 1:2) trellis.focus(panel, 1, 1) grid.rect(w = .5, gp = gpar(fill = light grey)) trellis.unfocus() The user-interface is a little rough, but this can be done by accessing the underlying grid objects. Here's an example, with explanatory bits interspersed ... # grab the lattice plot as a grid gTree # There are warnings, but they are ignorable latticeplot - grid.grabExpr(print(xyplot(x ~ x | gl(2,1), layout = 1:2))) # Demonstrate that the gTree faithfully replicates the # original lattice plot (not necessary, just to to what's going on) grid.newpage() grid.draw(latticeplot) # Explore the gTree (just to to show what's going on) # Better user-interface would be nice here ... childNames(latticeplot) # Identify which children are which # (appropriate grob names would be nice here) lapply(latticeplot$children, class) # Identify where each child is drawn latticeplot$childrenvp lapply(latticeplot$children, [[, vp) # Add a rect (starts off on top of everything else) # NOTE that rect has to have correct vpPath plotwithrect - addGrob(latticeplot, rectGrob(w = .5, gp = gpar(fill = light grey), vp=vpPath(plot1.toplevel.vp, plot1.panel.1.1.vp))) # Check this draws what we expect (just to show what's going on) grid.newpage() grid.draw(plotwithrect) # Reorder children to put rect at back # Appropriate user-interface would be nice here ... nc - length(plotwithrect$childrenOrder) plotwithrect$childrenOrder - plotwithrect$childrenOrder[c(nc, 1:(nc - 1))] # Final result grid.newpage() grid.draw(plotwithrect) Paul -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch 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] placing rectangle behind plot
Hi Gabor, On Sat, 29 Jul 2006 17:20:29 -0400, Gabor Grothendieck [EMAIL PROTECTED] wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? If you only need to draw the rectangle behind the points, why not 'panel.polygon' before 'panel.xyplot'? xyplot(x ~ x | gl(2, 1), layout=1:2, panel=function(x, y, ...) { panel.polygon(c(3, 3, 8, 8), c(0, 12, 12, 0), col=2) panel.xyplot(x, y, ...) }) Cheers, -- Seb __ R-help@stat.math.ethz.ch 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] placing rectangle behind plot
The reason I explicitly specified in the problem that the rectangle should not be drawn first is that the xyplot is issued as part of a larger routine that I don't want to modify. On 7/29/06, Sebastian P. Luque [EMAIL PROTECTED] wrote: Hi Gabor, On Sat, 29 Jul 2006 17:20:29 -0400, Gabor Grothendieck [EMAIL PROTECTED] wrote: I am trying to create a lattice plot and would like to later, i.e. after the plot is drawn, add a grey rectangle behind a portion of it. The following works except that the rectrangle is on top of and obscures a portion of the chart. I also tried adding col = transparent to the gpar list but that did not help -- I am on windows and perhaps the windows device does not support transparency? At any rate, how can I place the rectangle behind the plotted points without drawing the rectangle first? If you only need to draw the rectangle behind the points, why not 'panel.polygon' before 'panel.xyplot'? xyplot(x ~ x | gl(2, 1), layout=1:2, panel=function(x, y, ...) { panel.polygon(c(3, 3, 8, 8), c(0, 12, 12, 0), col=2) panel.xyplot(x, y, ...) }) Cheers, -- Seb __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.