为R中的绘图重叠着色

时间:2019-01-29 16:07:34

标签: r plot

我想可视化R中多个形状的重叠,并以图形方式突出显示重叠。

我设法将圆绘制成一个图,但是我不清楚这些是否是我可以进一步使用的对象,或者它们仅仅是绘制的对象

这画了圈...

plot(1, type="n", xlab="Niche dimension 1", ylab="Niche dimension 2", main="Niche properties", xlim=c(0,20), ylim=c(0,20))
sp1<-draw.circle(10,10,3, border="darkgreen", lwd=2,col="darkgreen", density=20, angle=90)
sp2<-draw.circle(8,8,2, border="red", lwd=2, col="red", density=20, angle=180)
legend(15,3, legend=c("species 1", "species 2", "niche overlap"), fill=c("darkgreen","red",), cex=0.6)

现在,我希望某些包装能够将2个圆与一个对象相交,然后可以绘制和绘制不同的颜色。但是我还没有找到一个真正有效的方法...

1 个答案:

答案 0 :(得分:2)

您可以使用空间功能来完成将圆形创建为对象的工作,然后继续使用空间方法来找到相交点等进行绘图。

这是一个例子。

library(sp)
library(rgeos)
point1 = data.frame(x=1,y=1)
coordinates(point1) = ~ x + y
circle1 = gBuffer(point1, width = 10)
point2 = data.frame(x=3,y=3)
coordinates(point2) = ~ x + y
circle2 = gBuffer(point2, width = 10)

overlap = gIntersection(circle1, circle2)
circle1Only = gDifference(circle1, circle2)
circle2Only = gDifference(circle2, circle1)
all = gUnion(circle1, circle2)

plot(all) # make sure all pieces are included
plot(circle1Only, col = 'red', add = T)
plot(circle2Only, col = 'blue', add=T)
plot(overlap, col = 'green', add= T)

enter image description here