[R] fill the area outside a polygon

David L Carlson dcarlson at tamu.edu
Wed Dec 2 16:19:13 CET 2015


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.


More information about the R-help mailing list