[R] shade overlapping portions of circles (or other shapes)

Greg Snow 538280 at gmail.com
Wed Aug 15 23:21:37 CEST 2012


A quick and simple way would be to fill both circles with a
semi-transparent color (does not work on all devices), then the
overlapping area will will show up as a different color/shade due to
the alpha blending.  You might want to redraw the circles without fill
after the filled ones so that the 1st circle outline drawn is not
overlaid by the interior of the other circle.  This will mean that the
circles will be filled with a color other than the background (but
that may be a bonus).

plot(1:10, 1:10, type='n', asp=1)
theta <- seq(0,2*pi, length=400)
x1 <- 3*cos(theta)+4
y1 <- 3*sin(theta)+4
x2 <- 2*cos(theta)+6
y2 <- 2*sin(theta)+6
polygon(x1,y1, col="#0000ff55")
polygon(x2,y2, col="#00ff0055")


There are some of the spatial packages that will find the intersection
or overlay of 2 polygons that you could use to find the polygon that
represents the intersection and fill it.

library(sp)
library(rgeos)
x1 <- c(x1,x1[1])
y1 <- c(y1,y1[1])
x2 <- c(x2,x2[1])
y2 <- c(y2,y2[1])
p1 <- SpatialPolygons(list(Polygons(list(Polygon(cbind(rev(x1),rev(y1)))),ID=1)))
p2 <- SpatialPolygons(list(Polygons(list(Polygon(cbind(rev(x2),rev(y2)))),ID=2)))
tmp <- gIntersection( p1, p2 )
plot(p1, xlim=c(1,10), ylim=c(1,10))
plot(p2, add=TRUE)
plot(tmp, add=TRUE, col='green')

I hope that there is a simpler way than above, but it works.

The last option is to use math to figure out the intersection and
polygon to fill it.


On Wed, Aug 15, 2012 at 10:43 AM, Paul Johnson <pauljohn32 at gmail.com> wrote:
> I'm making some illustrations and it would be convenient to
> automatically shade the overlapping portions of circles.  These
> illustrations are for Social Choice theory, a field in political
> science and economics.  I've wrestled together some examples so you
> can see what I mean, but have not mastered the "color overlapping
> sections" problem (as you will see):
>
> http://pj.freefaculty.org/scraps/t-m15g.pdf
>
> The shaded area does not overlap perfectly, mainly because I isolated
> the points manually with locator(). Tedious!
>
> I've not found R functions just for this.  Are there any packages?
>
> You can get an idea of the problem if you run, for example
>
> library(plotrix)
> example(draw.circle)
>
> See the red and the blue circles overlap?  What's the best way to
> "shade in" that overlapping area.
>
> I don't have Mathematica anymore, but they seem to be able to do this
> more-or-less easily
>
> http://mathematica.stackexchange.com/questions/2554/how-to-plot-venn-diagrams-with-mathematica
>
> I want to make R do same ( or develop package to do it).
>
> I was under some pressure to get a graph finished that I linked to
> above. But I would not want to do it that way again. The shapes won't
> always  be perfect circles, they may be ellipses or other convex
> shapes.  But if I could understand the problem for circles, maybe I
> could solve for others.
>
> Do you have suggestions?
>
> I am considering this. Collect the co-ordiates from the circle points
>
>  C1 <- draw.circle(3.5,7,0.8,border="blue",lty=2,lwd=2)
>  C2 <- draw.circle(2.5,8,0.6,border="red",lty=3,lwd=3)
>
> Can I isolate the relevant arcs from C1 and C2? If I could, I'd join them
>
> Cjoin <- list(x = c(C1$x, C2$x), y = c(C1$y, C2$y))
>
> And use that for plotting.
>
> pj
>
>
> -- Paul E. Johnson
> Professor, Political Science    Assoc. Director
> 1541 Lilac Lane, Room 504     Center for Research Methods
> University of Kansas               University of Kansas
> http://pj.freefaculty.org            http://quant.ku.edu
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list