Radar Plot

Data Science ggradar R

This is an interesting plor to compare variables in a lot of researches.

Published

Oct. 15, 2022

Citation

Santos, 2022

Building a radar plot

library(ggradar)
library(tidyverse)
library(tidyquant)
library(scales)
library(corrr)

Data

mpg
# A tibble: 234 × 11
   manuf…¹ model displ  year   cyl trans drv     cty   hwy fl    class
   <chr>   <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
 1 audi    a4      1.8  1999     4 auto… f        18    29 p     comp…
 2 audi    a4      1.8  1999     4 manu… f        21    29 p     comp…
 3 audi    a4      2    2008     4 manu… f        20    31 p     comp…
 4 audi    a4      2    2008     4 auto… f        21    30 p     comp…
 5 audi    a4      2.8  1999     6 auto… f        16    26 p     comp…
 6 audi    a4      2.8  1999     6 manu… f        18    26 p     comp…
 7 audi    a4      3.1  2008     6 auto… f        18    27 p     comp…
 8 audi    a4 q…   1.8  1999     4 manu… 4        18    26 p     comp…
 9 audi    a4 q…   1.8  1999     4 auto… 4        16    25 p     comp…
10 audi    a4 q…   2    2008     4 manu… 4        20    28 p     comp…
# … with 224 more rows, and abbreviated variable name ¹​manufacturer

Format

vehicle_summary_tbl = mpg %>%
  select(class, where(is.numeric), -year) %>%
  
  # Median values by vehicle class
  group_by(class) %>%
  summarise(
    across(displ:hwy, .fns = median)
  ) %>%
  ungroup() %>%
  
  # Prep for ggradar (make sure to scale to 0-1)
  rename(group = class) %>%
  mutate_at(vars(-group), rescale)

vehicle_summary_tbl
# A tibble: 7 × 5
  group      displ   cyl   cty   hwy
  <chr>      <dbl> <dbl> <dbl> <dbl>
1 2seater    1       1   0.286  0.8 
2 compact    0       0   1      1   
3 midsize    0.150   0.5 0.714  1   
4 minivan    0.275   0.5 0.429  0.6 
5 pickup     0.625   1   0      0   
6 subcompact 0       0   0.857  0.9 
7 suv        0.612   1   0      0.05

Radar plots

Single radar

vehicle_summary_tbl %>%
  ggradar()
vehicle_summary_tbl %>%
  ggradar(
    group.colours = palette_light() %>%
      unname(),
    plot.title = "MPG Comparison By Vehicle Class"
  )

Faceted Radar

vehicle_summary_tbl %>%
  ggradar() +
  
  # Facet
  facet_wrap(~ group, ncol = 3) +
  
  # Theme
  theme_void() +
  scale_color_tq() +
  theme(
    strip.text = element_text(
      size = 12,
      colour = "white",
      margin = margin(t = 5, b = 5)
    ),
    strip.background = element_rect(fill = "#2C3E50"),
    legend.position = "none"
  ) +
  
  #Title
  labs(title = "MPG Comparison By Vehicle Class")

Which vehicles are most similar?

Get Vehicle Similarity

vehicle_similarity_tbl = vehicle_summary_tbl %>%
  
  #Transpose
  pivot_longer(cols = -1) %>%
  pivot_wider(
    names_from = group,
    values_from = value
  ) %>%
  
  # Correlate and Arrange
  corrr::correlate() %>%
  mutate(across(where(is.numeric), .fns = ~replace_na(., 1))) %>%
  arrange(`2seater`)

vehicle_similarity_tbl
# A tibble: 7 × 8
  term       `2seater` compact midsize minivan pickup subcomp…¹    suv
  <chr>          <dbl>   <dbl>   <dbl>   <dbl>  <dbl>     <dbl>  <dbl>
1 compact       -0.783   1       0.857   0.535 -0.951     0.999 -0.943
2 subcompact    -0.761   0.999   0.868   0.553 -0.950     1     -0.941
3 midsize       -0.468   0.857   1       0.894 -0.691     0.868 -0.663
4 minivan       -0.100   0.535   0.894   1     -0.301     0.553 -0.261
5 pickup         0.744  -0.951  -0.691  -0.301  1        -0.950  0.999
6 suv            0.764  -0.943  -0.663  -0.261  0.999    -0.941  1    
7 2seater        1      -0.783  -0.468  -0.100  0.744    -0.761  0.764
# … with abbreviated variable name ¹​subcompact

Reorder By Similarity

vehicle_summary_tbl %>%
  mutate(group = factor(group, levels = vehicle_similarity_tbl$term)) %>%
  ggradar()+
  facet_wrap(~group, ncol = 3)+
  scale_color_tq()+
  labs(title = "Vehicle Classes Arranged By Similarity") +
  theme(
    legend.position = "none",
    strip.background = element_rect(fill = "#2C3E50"),
    strip.text = element_text(color = "white")
    
  )

Coffee data analysis

# A tibble: 3 × 11
  group    Acidez Aroma Balance Cuerpo Dulzura General Limpi…¹ Regusto
  <chr>     <dbl> <dbl>   <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 Espesor…      1 0         0.5      1     0.5       1     0.5       1
2 Espesor…      0 0.500     0.5      1     0.5       1     0.5       0
3 Espesor…      1 1         0.5      0     0.5       0     0.5       1
# … with 2 more variables: Sabor <dbl>, Uniformidad <dbl>, and
#   abbreviated variable name ¹​Limpieza_Taza

Radar coffee quality plots

Single radar

Faceted Radar

Footnotes

    Citation

    For attribution, please cite this work as

    Santos (2022, Oct. 15). Franklin Santos: Radar Plot. Retrieved from https://franklinsantos.com/posts/2022-10-23-ggradarplot/

    BibTeX citation

    @misc{santos2022radar,
      author = {Santos, Franklin},
      title = {Franklin Santos: Radar Plot},
      url = {https://franklinsantos.com/posts/2022-10-23-ggradarplot/},
      year = {2022}
    }