rradar-chartspider-chart

How to create radar chart (spider chart)? can be done by ggplot2?


I have 11 sites (A-K) and every site I calculated the average scores on 6 elements and the average for all elements

      PCC  V1    V2    V3    V4    V5    V6     V7   Vtotal
    1  A  8.67  4.67  6.42  6.92  7.67  6.93   5.72  6.71 
    2  B  6.58  4.67  5.75  3.12  4.67  4.80   5.25  4.98
    3  C  6.50  5.67  7.25  5.75  5.33  6.40  4.00  5.84
    4  D  6.25  5.83  6.00  6.12  4.00  5.00  5.33  5.51
    5  E  9.00  5.67  6.50  8.00  6.17  3.60  5.00  6.28   
    6  F  8.92  7.00  6.62  5.75  7.17  5.90  6.67  6.86
    7  G  5.67  5.83  6.00  5.75  4.92  5.90  4.58  5.52
    8  H  8.92  7.50  9.62  6.50  6.17  7.60  7.33  7.66
    9  I  7.83  4.83  7.12  7.62  6.17  5.40  5.75  6.39
    10 J  7.50  7.67  7.25  8.38  7.17  6.30  7.00  7.32
    11 K  6.83  5.83  5.38  5.12  5.58  6.20  6.17  5.87

I want to draw a radar chart for each site and score ranges from 1-11 I've tried this function:

create_beautiful_radarchart <- function(data, color = "#00AFBB", 
                                        vlabels = colnames(data), vlcex = 0.7,
                                        caxislabels = NULL, title = NULL, ...){
  radarchart(
    data, axistype = 1,
    # Customize the polygon
    pcol = color, pfcol = scales::alpha(color, 0.5), plwd = 2, plty = 1,
    # Customize the grid
    cglcol = "grey", cglty = 1, cglwd = 0.8,
    # Customize the axis
    axislabcol = "grey", 
    # Variable labels
    vlcex = vlcex, vlabels = vlabels,
    caxislabels = caxislabels, title = title, ...
  )
}

Then I created a specific data frame for each site:

PCCA = df[1,2:9]
PCCB = df[2,2:9] ...

Then I tried this:

create_beautiful_radarchart( data = PCCA, caxislabels = c(0,1,2,3,4,5,6,7,8,9,10,11))

But I did not get the chart as needed (attached photo)spider chart


Solution

  • Provided I understood you correctly, I'd start with something like this:

    library(tidyverse)
    
    # Thanks to: https://stackoverflow.com/questions/42562128/ggplot2-connecting-points-in-polar-coordinates-with-a-straight-line-2
    coord_radar <- function (theta = "x", start = 0, direction = 1) {
        theta <- match.arg(theta, c("x", "y"))
        r <- if (theta == "x") "y" else "x"
        ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
                direction = sign(direction),
                is_linear = function(coord) TRUE)
    }
    
    df %>%
        pivot_longer(-PCC) %>%
        ggplot(aes(x = name, y = value, colour = PCC, group = PCC)) + 
        geom_line() +
        coord_radar() + 
        theme_minimal()
    

    enter image description here

    To generate separate plots per PCC I'd use facets

    df %>%
        pivot_longer(-PCC) %>%
        ggplot(aes(x = name, y = value, group = PCC)) + 
        geom_line() +
        coord_radar() + 
        facet_wrap(~ PCC) +
        theme_minimal()
    

    enter image description here


    Sample data

    df <- read.table(text = "  PCC  V1    V2    V3    V4    V5    V6     V7   Vtotal
        1  A  8.67  4.67  6.42  6.92  7.67  6.93   5.72  6.71 
        2  B  6.58  4.67  5.75  3.12  4.67  4.80   5.25  4.98
        3  C  6.50  5.67  7.25  5.75  5.33  6.40  4.00  5.84
        4  D  6.25  5.83  6.00  6.12  4.00  5.00  5.33  5.51
        5  E  9.00  5.67  6.50  8.00  6.17  3.60  5.00  6.28   
        6  F  8.92  7.00  6.62  5.75  7.17  5.90  6.67  6.86
        7  G  5.67  5.83  6.00  5.75  4.92  5.90  4.58  5.52
        8  H  8.92  7.50  9.62  6.50  6.17  7.60  7.33  7.66
        9  I  7.83  4.83  7.12  7.62  6.17  5.40  5.75  6.39
        10 J  7.50  7.67  7.25  8.38  7.17  6.30  7.00  7.32
        11 K  6.83  5.83  5.38  5.12  5.58  6.20  6.17  5.87", header = T)