# [R] fill the area outside a polygon

Michael Sumner mdsumner at gmail.com
Wed Dec 2 21:38:44 CET 2015

```On Wed, 2 Dec 2015 at 23:10 Adrian Dușa <dusa.adrian at unibuc.ro> wrote:

> Dear All,
>
> I know how to fill a polygon, using a basic R graphics device:
>
> par(mai=c(0, 0, 0, 0))
> plot(1:100, type="n")
> polygon(c(20, 80, 80, 20), c(20, 20, 80, 80), col="lightblue")
>
>
> But let's say I have an outer polygon like this:
> polygon(c(0,100,100,0), c(0,0,100,100))
>
> Is it possible to fill the area between the two polygons?
>
>
Try polypath with the evenodd rule (vs. the winding rule):

p1 <- list(x = c(20, 80, 80, 20), y= c(20, 20, 80, 80))
p2 <- list(x = c(0,100,100,0), y = c(0,0,100,100))
plot(p2, type="n")
polypath(x = c(p1\$x, NA_real_, p2\$x), y = c(p1\$y, NA_real_, p2\$y), col =
"lightblue", rule = "evenodd")

Cheers, Mike.

> I realise there is a quick and dirty solution to fill the outer polygon,
> then draw the inner polygon colored with white, but I would like to
> actually "clip" the outer polygon to the boundaries of the inner one.
>
> Of course, there are various packages which draw maps (i.e. maptools), but
> I need this in base R graphics. In other words, is it possible to define a
> polygon with a hole inside, in base R?
>
> Thank you,
>
> --
> University of Bucharest
> Romanian Social Data Archive
> Soseaua Panduri nr.90
> 050663 Bucharest sector 5
> Romania
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help