Skip to content
Prev 277872 / 398503 Next

How to fill irregular polygons with patterns?

Hi, 

I'm looking the best way to fill irregular polygons with patterns,
Something like the function grid.pattern do, but my case is with
irregular polygons.

Whit this script I can get it, but I'm looking for an "elegant" solution..

library(grid)

grid.polygon(x=c(0.2, 0.8, 0.6, 0.6, 0.8, 0.2), 
    y=c(0.2, 0.2, 0.3, 0.5, 0.7,0.7), 
    gp=gpar(fill="grey", alpha=0.9))
tmp=seq(0.225,0.7,0.075)
for(i in 1:length(tmp)) {
  grid.abline(intercep=tmp[i], 
  slope=0, units="npc",
  range=c(0.21,0.79), 
  gp=gpar(lty=c("F8","2"), lwd=1.1))
}
tmp2=seq(0.265,0.685,0.075)
for(i in 1:length(tmp2)) {
  grid.abline(intercep=tmp2[i], 
  slope=0, units="npc",
  range=c(0.225,0.775), 
  gp=gpar(lty="F8", lwd=1.1, col="red"))
}

grid.polygon(x=c(0.8, 0.6, 0.6, 0.8), 
  y=c(0.2, 0.3, 0.5, 0.7), 
  gp=gpar(fill="white", col="white"))

grid.polygon(x=c(0.2, 0.8, 0.6, 0.6, 0.8, 0.2), 
  y=c(0.2, 0.2, 0.3, 0.5, 0.7,0.7), 
  gp=gpar(fill="transparent"))

###  other example


grid.polygon(x=c(0.2, 0.8, 0.6, 0.6, 0.8, 0.2), 
  y=c(0.2, 0.2, 0.3, 0.5, 0.7,0.7), 
  gp=gpar(fill="yellow", alpha=0.8))

grid.points(x=runif(500, min=0.21, max=0.79), 
  y=runif(500, min=0.21, max=0.69), 
  default.units="npc", pch=c(20,21), 
  gp=gpar(cex=c(0.3, 0.5, 0.6), alpha=0.7))

grid.polygon(x=c(0.8, 0.6, 0.6, 0.8), 
  y=c(0.2, 0.3, 0.5, 0.7), 
  gp=gpar(fill="white", col="white", 
  lwd=0, alpha=1))

grid.polygon(x=c(0.2, 0.8, 0.6, 0.6, 0.8, 0.2), 
  y=c(0.2, 0.2, 0.3, 0.5, 0.7,0.7), 
  gp=gpar(fill="transparent", alpha=1))


Thanks in advance,

John