19

Several functions in R's base graphical system, including rect() and polygon(), support cross-hatching via their angle= and density= arguments:

x = c(0, 0.5, 1, 0.5)
y = c(0.5, 1, 0.5, 0)
par(mar=c(0,0,0,0))
plot.new()
polygon(x, y, angle=45, density=10)

enter image description here

How might I apply similar cross-hatching to a polygon drawn by the grid graphical system's grid.polygon() function:

library(grid)
grid.newpage()
grid.polygon(x,y)

enter image description here

I've looked in the documentation for ?grid.polygon and ?gpar, and have skimmed through Paul Murrel's book on R graphics, and have so far come up empty. Am I missing something obvious? If not, is there some simple hack which will make this possible?

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 4
    it's not possible. It's however possible via gridSVG (`grid.patternFill`) – baptiste Sep 29 '14 at 23:03
  • @baptiste -- Good to know. Coming from you, I'll take that as +/definitive. Thanks, too, for the pointer to that gridSVG function. – Josh O'Brien Sep 29 '14 at 23:06
  • 1
    i never really tried, and it's relatively recent I think. Here's an example, https://www.stat.auckland.ac.nz/~paul/Talks/London2013/barchart/barchart.R – baptiste Sep 29 '14 at 23:08
  • @baptiste Unfortunately, I need this in a pdf, so I might just have to convert my polygons to a raster, and use that as a mask to extract a pattern from another raster image (as demonstrated in [this Paul Murrel R-Journal article](http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdf)). Looks like your `gridExtra::rpatternGrob` might be helpful if I go in that direction. – Josh O'Brien Sep 29 '14 at 23:30
  • I would strongly recommend against using (r)patternGrob, they're hopeless (and won't work for general polygons, just rectangles) – baptiste Sep 29 '14 at 23:39
  • Alternatively, since I'm actually dealing with spatial data (and my polygons are already `SpatialPolygons`), I suppose I could construct a `SpatialLines` object and use **rgeos** to take its intersection with my polygons. In theory, that'd supply a general (if quite roundabout) way to get hatching for polygons. – Josh O'Brien Sep 29 '14 at 23:42
  • 2
    personally, I'd give svg-to-pdf conversion a shot, maybe [some recent tools](http://www.cairosvg.org/) or even Inkscape can do a decent job. – baptiste Sep 29 '14 at 23:48

1 Answers1

22

Here's an example with gridSVG adapted from Paul Murrell's presentation

library(gridSVG)
library(grid)
x = c(0, 0.5, 1, 0.5)
y = c(0.5, 1, 0.5, 0)
grid.newpage()
grid.polygon(x,y, name="goodshape")

pat <- pattern(linesGrob(gp=gpar(col="black",lwd=3)),
  width = unit(5, "mm"), height = unit(5, "mm"),
  dev.width = 1, dev.height = 1)

# Registering pattern
registerPatternFill("pat", pat)
# Applying pattern fill
grid.patternFill("goodshape", label = "pat")

grid.export("test-pattern.svg")

enter image description here

more complex grobs are allowed as well, since svg takes care of the clipping.

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294