Creating and splitting polygon in R

520 views Asked by At

I'm trying to create 2 circles and then split them into 4 parts so that I get 8 sectors, then i want to attribute a value from 0-1 to each sector and colour the circle based on the value given to it.

Thus I have a vector

    ratio<-c(0.02,0.04,0.67,0.9874,0.134552,0.753,0.9754,0.23460)

Which contains the ratio of each sector and need to attribute these with the sector and colour sector based on these values.

I have been able to create the circle but its just one polygon, how do i separate the polygon into 8 sectors then attribute a value to it? or maybe create a numbering system to number the sectors ?

I have been searching online for a sometime now can't find the answer.

I have used the following code to create the polygon,

   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 code is for creating the separate polygons

  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]) ))

I was advised to use splancs package.

I appreciate any suggestion.

I have been able to draw the polygons BUT still can't colour the plotted polygon based on the ratio vector. I need the colour to reflect the ratio.

1

There are 1 answers

4
Rorschach On BEST ANSWER

Here is a possibility, using base 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

enter image description here