rradar-chartspider-chart

how to calculate area of polygon in ggradar


I'm hoping to calculate the area of a polygon using ggradar.

From the research I've done so far, I think the goal is to calculate the area of each triangle and add them all together. Since we know the distance from the centroid to each point and the angle between the two sides of each triangle, we can calculate the area for each wedge using side1 x side2 x sin(pi/n)/2 (where n = number of variables, in this case 4) and adding all the wedges together.

Using mtcars data as an example:

mtcars_radar <- mtcars %>% 
  as_tibble(rownames = "group") %>% 
  mutate_at(vars(-group), rescale) %>% 
  tail(1) %>% 
  select(1:5)

ggradar(mtcars_radar)+
  theme(legend.position='right')

radar chart using mtcars

So then my issue is how to automatically calculate the area of each triangle and add them together. Any suggestions on how to do this? Thanks in advance.


Solution

  • While I question the utility, it certainly is possible to do this in R. Here's such a function

    radar_area <- function(data) {
      vals <- data[, -1, drop=FALSE] # assume the first column is the ID
      first <- 1:ncol(vals)
      second <- c(first[-1], first[1])
      Reduce(`+`, Map(function(i, j) vals[,i] * vals[,j], first, second))/2 * sin(2*pi/ncol(vals))
    }
    
    radar_area(mtcars_radar)
    

    We pair up all ends of the triangles, and then we multiple them, then add and adjust according to the angle between them. This assumes the center of the circle is at 0,0 but in the sample plot provided it appears the radar plot does not put 0 in the exact center of the image. There seems to be an offset. If necessary, you could add the length of that offset to the triangle lengths.