1
votes

I'm trying to produce something similar to this chart from the moonBook package as shown here

enter image description here

My data regards participation in telework programs for several dozen organizations, and all I have are gross percentages, not individual responses.

So for Organization X, I know:

Telework rate: 33%
No telework rate: 67%

For those who do telework, their frequencies are:

Very infrequently: 19.8%
1-2 days/month: 4.8%
1-2 days/week: 7.9%
3-4 days/week: 0.4%
Every work day: 0.1%

For those who do not telework, their reasons are:

Must be physically present: 11.5%
Technical limitations: 7.1%
Not authorized: 25.5%
Choose not to: 23.9%

I'd like the central pie to show the telework/no-telework breakout for a given organization and the outer donut to show the frequency/reason. But I'm at a loss as to how to proceed as virtually every example I've found begins from individual responses.

1

1 Answers

3
votes

Here is a solution that might be really close to what you are looking for.

I used some data similar to what you showed:

df<-data.frame(Org=c("Tele",rep(c("Tele","NotTele"),each=4)),
                     CatFreq = LETTERS[1:9],
               Freq = c(19.8,4.8,7.9,0.4,0.1,11.5,7.1,25.5,22.9))

The rest of the code

library(tidyverse)

df %>%
  #Calculate cumulative sums
  mutate(cumsum = cumsum(Freq),
         min = cumsum-Freq) %>%
  #Calculate cumulative sums by Org
  group_by(Org) %>%
  mutate(cumsum2 = max(cumsum(Freq))) %>%
  ungroup() %>%
  #Get mid point to print labels for Org levels
  mutate(mid = ifelse(Org == "Tele", cumsum2 / 2, min(cumsum2) + (max(cumsum2)/2) ) ) %>%
  #Do the ggplot by doing two geom_rect, 1 for Org, 1 for CatFreq
  ggplot()+
  geom_rect(aes(xmin = 1,
                xmax = 2,
                ymin = min, 
                ymax = cumsum,
                fill = Org))+
  #Add labels for Org
  geom_text(aes(x = 1 + ((2 - 1)/2), 
                y = mid,
                label = paste(Org, paste0(cumsum2, "%"), sep= "\n"))) +
  geom_rect(aes(xmin = 2,
                xmax = 3,
                ymin = min, 
                ymax = cumsum,
                fill = CatFreq))+
  #Add labels for CatFreq
  geom_text(aes(x = 2 + ((3 - 2)/2), 
                y = min + ((cumsum - min)/2),
                label = paste(CatFreq, paste0(Freq, " %"), sep= "\n"))) +
  #Changing the plot to pie
  coord_polar("y", start = 0) +
  #Making it a doughnut
  xlim(c(0,4)) + 
  #Set theme void
  theme_void() +
  #Eliminate legend
  theme(legend.position = "none")

enter image description here