我想知道是否有人对如何计算我的信心椭圆内的蓝色阴影区的想法。 任何建议或地方看看是极大的赞赏。 另外,我希望能找到一个通式,因为椭圆不必撒谎,在应用程序的区域(即,椭圆可能是更大)。 这是我为我的图片的代码是否有帮助:
library(car)
x = c(7,4,1)
y = c(4,6,7)
plot(x,y,xlim=c(0,10),ylim=c(0,10))
rect(x,y,max(x)+100000,max(y)+100000,col="lightblue",border=NA)
points(x,y,col="red",pch=19)
ellipse(center=c(3.5,5),shape=matrix(c(1,.5,.5,2),nrow=2),radius=3,col="green")
如果你可以转换椭圆和蓝色区域SpatialPolygons
对象,那么这是小事一桩,使用从rgeos包功能,计算它们的面积,它们的交集区域,和所有其他各种有趣量。
不幸的是,在适当的形式获取对象需要一些繁重的工作:
library(car)
library(sp)
library(rgeos)
## Function for creating a SpatialPolygons object from data.frame of coords
xy2SP <- function(xy, ID=NULL) {
if(is.null(ID)) ID <- sample(1e12, size=1)
SpatialPolygons(list(Polygons(list(Polygon(xy)), ID=ID)),
proj4string=CRS("+proj=merc"))
}
## Ellipse coordinates
plot.new() # Needed by ellipse()
ell <- ellipse(center=c(3.5,5),shape=matrix(c(1,.5,.5,2),nrow=2),radius=3)
dev.off() # Cleaning up after plot.new()
## Three rectangles' coordinates in a length-3 list
x <- c(7,4,1)
y <- c(4,6,7)
mx <- max(x) + 1e6
my <- max(y) + 1e6
rr <- lapply(1:3, function(i) {
data.frame(x = c(x[i], x[i], mx, mx, x[i]),
y = c(y[i], my, my, y[i], y[i]))
})
## Make two SpatialPolygons objects from ellipse and merged rectangles
ell <- xy2SP(ell)
rrr <- gUnionCascaded(do.call(rbind, lapply(rr, xy2SP)))
## Find area of their intersection
gArea(gIntersection(ell, rrr))
# [1] 10.36296