在 R 中创建和拆分多边形
Creating and splitting polygon in R
我正在尝试创建 2 个圆圈,然后将它们分成 4 个部分,这样我就得到 8 个扇区,然后我想为每个扇区分配一个 0-1 的值,并根据给定的值为圆圈着色给它。
因此我有一个向量
ratio<-c(0.02,0.04,0.67,0.9874,0.134552,0.753,0.9754,0.23460)
其中包含每个扇区的比率,需要根据这些值将它们与扇区和颜色扇区一起归属。
我已经能够创建圆,但它只有一个多边形,我如何将多边形分成 8 个扇区然后为其赋值?或者创建一个编号系统来对扇区进行编号?
我已经在网上搜索了一段时间,现在找不到答案。
我使用以下代码创建了多边形,
circle1<-0
circle2=0
x1=0
y1=0
k=0
y2<-0
x2<-0
for(l in 1:2){
for(j in 1:4){
for(i in 0:180){
x1<-c(x1,l*cos(i*0.0087266462599716+k*180*0.0087266462599716))
y1<-c(y1,l*sin(i*0.0087266462599716+k*180*0.0087266462599716))
}
x2<-c(x2,x1,0)
y2<-c(y2,y1,0)
x1<-0
y1<-0
k=k+1
}
circle1<-c(circle1,x2)
circle2<-c(circle2,y2)
}
circle3<-cbind(circle1,circle2)
polymap(circle3,axes=T)
#below 代码用于创建单独的多边形
polys <- SpatialPolygons(list(
Polygons(list(Polygon(matrix(c(circle3[1:183,1],
circle3[1:183,2]), ncol=2))),c[1]),
Polygons(list(Polygon(matrix(c(circle3[184:366,1],
circle3[184:366,2]), ncol=2))), c[2]) ))
有人建议我使用 splancs 包。
我很感激任何建议。
我已经能够绘制多边形,但仍然无法根据比率向量为绘制的多边形着色。我需要颜色来反映比例。
这里有一个可能性,使用基数 R,
## Get circle points
circs <- function(radii, sectors=4) {
radii <- sort(radii)
rads <- seq(0, 2*pi, length=100*length(radii)*sectors) # sample at these radians
do.call(rbind, lapply(radii, function(r) # points for drawing circles
data.frame(X=r*cos(rads), Y=r*sin(rads),
sector=rep(1:sectors, each=length(rads)/sectors),
theta=rads, radius=r)))
}
## Draw figure
drawCirc <- function(radii, sectors, hues=NULL, densities=NULL, ...) {
polys <- circs(radii, sectors)
if (missing(hues)) {
colors <- colorRampPalette(c("green","yellow","red"))(sectors*length(radii))
} else
colors <- heat.colors(n=sectors*length(radii), alpha=hues)
plot(polys[,1:2], type="n", ...) # blank plot
for (i in seq_along(radii)) { # add polygons
for (j in 1:sectors) {
ind <- (i-1)*length(radii)+j
color <- colors[ind]
with(polys[polys$sector==j,],
if (i == 1) {
polygon(x=c(0, X[radius==radii[i]], 0), y=c(0, Y[radius==radii[i]], 0),
col=color, density=densities[ind])
} else
polygon(x=c(X[radius==radii[i-1]], rev(X[radius==radii[i]])),
y=c(Y[radius==radii[i-1]], rev(Y[radius==radii[i]])),
col=color, density=densities[ind]))
}
}
}
## Figures
par(mfrow = c(2,2))
## With no shading by ratio - not run
drawCirc(radii=1:2, sectors=4, main="No ratio")
drawCirc(radii=c(0.1, 0.5, 0.9), sectors=7, main="No Ratio, Varying Radii/Sectors")
## Density/Color by ratio
## ** You would need to make sure the ratio vector is in the proper order **
ratio <- c(0.02,0.04,0.67,0.9874,0.134552,0.753,0.9754,0.23460)
drawCirc(radii=1:2, sectors=4, den=ratio*50, main="Ratio by Density") # by density
drawCirc(radii=1:2, sectors=4, hues=ratio, main="Ratio by Colors") # heat map
我正在尝试创建 2 个圆圈,然后将它们分成 4 个部分,这样我就得到 8 个扇区,然后我想为每个扇区分配一个 0-1 的值,并根据给定的值为圆圈着色给它。
因此我有一个向量
ratio<-c(0.02,0.04,0.67,0.9874,0.134552,0.753,0.9754,0.23460)
其中包含每个扇区的比率,需要根据这些值将它们与扇区和颜色扇区一起归属。
我已经能够创建圆,但它只有一个多边形,我如何将多边形分成 8 个扇区然后为其赋值?或者创建一个编号系统来对扇区进行编号?
我已经在网上搜索了一段时间,现在找不到答案。
我使用以下代码创建了多边形,
circle1<-0
circle2=0
x1=0
y1=0
k=0
y2<-0
x2<-0
for(l in 1:2){
for(j in 1:4){
for(i in 0:180){
x1<-c(x1,l*cos(i*0.0087266462599716+k*180*0.0087266462599716))
y1<-c(y1,l*sin(i*0.0087266462599716+k*180*0.0087266462599716))
}
x2<-c(x2,x1,0)
y2<-c(y2,y1,0)
x1<-0
y1<-0
k=k+1
}
circle1<-c(circle1,x2)
circle2<-c(circle2,y2)
}
circle3<-cbind(circle1,circle2)
polymap(circle3,axes=T)
#below 代码用于创建单独的多边形
polys <- SpatialPolygons(list(
Polygons(list(Polygon(matrix(c(circle3[1:183,1],
circle3[1:183,2]), ncol=2))),c[1]),
Polygons(list(Polygon(matrix(c(circle3[184:366,1],
circle3[184:366,2]), ncol=2))), c[2]) ))
有人建议我使用 splancs 包。
我很感激任何建议。
我已经能够绘制多边形,但仍然无法根据比率向量为绘制的多边形着色。我需要颜色来反映比例。
这里有一个可能性,使用基数 R,
## Get circle points
circs <- function(radii, sectors=4) {
radii <- sort(radii)
rads <- seq(0, 2*pi, length=100*length(radii)*sectors) # sample at these radians
do.call(rbind, lapply(radii, function(r) # points for drawing circles
data.frame(X=r*cos(rads), Y=r*sin(rads),
sector=rep(1:sectors, each=length(rads)/sectors),
theta=rads, radius=r)))
}
## Draw figure
drawCirc <- function(radii, sectors, hues=NULL, densities=NULL, ...) {
polys <- circs(radii, sectors)
if (missing(hues)) {
colors <- colorRampPalette(c("green","yellow","red"))(sectors*length(radii))
} else
colors <- heat.colors(n=sectors*length(radii), alpha=hues)
plot(polys[,1:2], type="n", ...) # blank plot
for (i in seq_along(radii)) { # add polygons
for (j in 1:sectors) {
ind <- (i-1)*length(radii)+j
color <- colors[ind]
with(polys[polys$sector==j,],
if (i == 1) {
polygon(x=c(0, X[radius==radii[i]], 0), y=c(0, Y[radius==radii[i]], 0),
col=color, density=densities[ind])
} else
polygon(x=c(X[radius==radii[i-1]], rev(X[radius==radii[i]])),
y=c(Y[radius==radii[i-1]], rev(Y[radius==radii[i]])),
col=color, density=densities[ind]))
}
}
}
## Figures
par(mfrow = c(2,2))
## With no shading by ratio - not run
drawCirc(radii=1:2, sectors=4, main="No ratio")
drawCirc(radii=c(0.1, 0.5, 0.9), sectors=7, main="No Ratio, Varying Radii/Sectors")
## Density/Color by ratio
## ** You would need to make sure the ratio vector is in the proper order **
ratio <- c(0.02,0.04,0.67,0.9874,0.134552,0.753,0.9754,0.23460)
drawCirc(radii=1:2, sectors=4, den=ratio*50, main="Ratio by Density") # by density
drawCirc(radii=1:2, sectors=4, hues=ratio, main="Ratio by Colors") # heat map