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? 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, Adrian -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]
Using only base graphics, one solution would be to embed the inner polygon in the outer one and turn off the border: par(mai=c(0, 0, 0, 0)) plot(1:100, type="n") polygon(c(0, 100, 100, 0, 0, 20, 80, 80, 20, 20, 0), c(0, 0, 100, 100, 0, 20, 20, 80, 80, 20, 0), col="lightblue", border=NA) ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Adrian Du?a Sent: Wednesday, December 2, 2015 6:07 AM To: r-help at r-project.org Subject: [R] fill the area outside a polygon 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? 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, Adrian -- Adrian Dusa 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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Wed, Dec 2, 2015 at 5:19 PM, David L Carlson <dcarlson at tamu.edu> wrote:> > Using only base graphics, one solution would be to embed the innerpolygon in the outer one and turn off the border:> > par(mai=c(0, 0, 0, 0)) > plot(1:100, type="n") > polygon(c(0, 100, 100, 0, 0, 20, 80, 80, 20, 20, 0), > c(0, 0, 100, 100, 0, 20, 20, 80, 80, 20, 0), col="lightblue",border=NA) I see what you did, interesting. I do need the border though, although if this is the only solution, perhaps I could live without it. It's working on a Windows machine (which I presume you are using), but under MacOS I only get the entire big polygon filled, including the hole in the middle. Thanks, Adrian -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]
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, > Adrian > > -- > Adrian Dusa > 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 > 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]]
On Wed, Dec 2, 2015 at 10:38 PM, Michael Sumner <mdsumner at gmail.com> wrote:> > 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): >Oh, that is sweet! I didn't even know about the polypath() function. Time to study now... Thanks a lot! Adrian -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]