For a while, I’ve been wanting to work with donut charts. I had to upload to R 4.0 in order to use the really cool donut charts from the webr package. The main function is PieDonut() and builds a pie chart + a donut chart with options to explode the different pieces of the pie.

Even better than PieDonut() however, is the interactivity found in the sunburstR package. Wow!

Loading in the Data

astronauts <- read_csv("astronauts.csv") 

astronauts <- astronauts %>%
  mutate(job_title = case_when(
    str_detect(occupation, "(?i)Other") ~ "other",
    str_detect(occupation, "(?i)Space") ~ "other",
    str_detect(occupation, "(?i)pilot") ~ "pilot",
    str_detect(occupation, "engineer") ~ "flight engineer",
    TRUE ~ occupation
  ))

head(astronauts)
## # A tibble: 6 x 25
##      id number nationwide_numb… name  original_name sex   year_of_birth
##   <dbl>  <dbl>            <dbl> <chr> <chr>         <chr>         <dbl>
## 1     1      1                1 Gaga… ГАГАРИН Юрий… male           1934
## 2     2      2                2 Tito… ТИТОВ Герман… male           1935
## 3     3      3                1 Glen… Glenn, John … male           1921
## 4     4      3                1 Glen… Glenn, John … male           1921
## 5     5      4                2 Carp… Carpenter, M… male           1925
## 6     6      5                2 Niko… НИКОЛАЕВ Анд… male           1929
## # … with 18 more variables: nationality <chr>, military_civilian <chr>,
## #   selection <chr>, year_of_selection <dbl>, mission_number <dbl>,
## #   total_number_of_missions <dbl>, occupation <chr>, year_of_mission <dbl>,
## #   mission_title <chr>, ascend_shuttle <chr>, in_orbit <chr>,
## #   descend_shuttle <chr>, hours_mission <dbl>, total_hrs_sum <dbl>,
## #   field21 <dbl>, eva_hrs_mission <dbl>, total_eva_hrs <dbl>, job_title <chr>

PieDonut() function from webr

PieDonut(astronauts,aes(pie=job_title, donut=sex), 
         labelposition = 4, r0=1.2, r1 = 5, r2 = 5.5, pieLabelSize = 3,
         family = "mono") 

PieDonut(astronauts,aes(pies=sex,donuts=military_civilian),
         selected=1,labelposition=1)

sunburstR

Then I found the interactive functionality of the functions sunburst() and sund2b() which are both pretty amazing.

https://github.com/timelyportfolio/sunburstR

https://timelyportfolio.github.io/sunburstR/articles/sunburst-2-0-0.html

library(sunburstR)
library(d3r)

sundata <- astronauts %>%
  select(sex, job_title, military_civilian) %>%
  group_by(sex, job_title, military_civilian) %>%
  summarize(size = n()) %>%
  d3_nest(value_cols = "size")
sunburst(sundata)
Legend

The sund2b() function has a lot more interactivity. Try clicking around the image to see various different functionality.

sund2b(sundata)