Order pie chart slices in ggplot2

Viewed 284

I want to order my pie chart labels and slices. My attempt below was able to order the labels, but the slices are still unordered. Thank you for your help.

enter image description here

data = structure(list(Stage = c("Collecting Applications", "Interview Stages", 
"Offer Accepted", "Offer in Progress", "To Open"), N = c(17L, 
30L, 8L, 2L, 65L), Prop = c(14, 25, 7, 2, 53), Label = c("14%", 
"25%", "7%", "2%", "53%")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

data$Stage = factor(data$Stage, ordered = TRUE, 
                      levels = c("To Open", "Collecting Applications", "Interview Stages", "Offer in Progress", "Offer Accepted"))

library(dplyr)
data = data %>% 
    mutate(end = 2 * pi * cumsum(N)/sum(N),
           start = lag(end, default = 0),
           middle = 0.5 * (start + end),
           hjust = ifelse(middle > pi, 1, 0),
           vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))
data = data[order(data$Stage), ]
  
  
library(ggforce)
library(ggplot2)

ggplot(data) + 
    geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                     start = start, end = end, fill = Stage)) +
    geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = Label,
                  hjust = hjust, vjust = vjust)) +
    coord_fixed() +
    scale_x_continuous(limits = c(-1.5, 1.4),  # Adjust so labels are not cut off
                       name = "", breaks = NULL, labels = NULL) +
    scale_y_continuous(limits = c(-1.2, 1.2),      # Adjust so labels are not cut off
                       name = "", breaks = NULL, labels = NULL) +
    theme_void() +
    labs(title = paste0("Progress for ", sum(data$N), " Positions"),
         fill = NULL) +
    scale_fill_brewer(palette = "Blues")

1 Answers

A simple fix, the order data frame line needs to be before the hjust and vjust mutations. Although not entirely sure of the reasoning behind it. Would appreciate an explanation if someone knows. Thanks!

library(dplyr)
data$Stage = factor(data$Stage, ordered = TRUE, 
                      levels = c("To Open", "Collecting Applications", "Interview Stages", "Offer in Progress", "Offer Accepted"))
data = data[order(data$Stage), ]
data = data %>% 
    mutate(end = 2 * pi * cumsum(N)/sum(N),
           start = lag(end, default = 0),
           middle = 0.5 * (start + end),
           hjust = ifelse(middle > pi, 1, 0),
           vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))
  
  
  
library(ggforce)
library(ggplot2)
  
ggplot(data) + 
    geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                     start = start, end = end, fill = Stage)) +
    geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = Label,
                  hjust = hjust, vjust = vjust)) +
    coord_fixed() +
    scale_x_continuous(limits = c(-1.5, 1.4),  # Adjust so labels are not cut off
                       name = "", breaks = NULL, labels = NULL) +
    scale_y_continuous(limits = c(-1.2, 1.2),      # Adjust so labels are not cut off
                       name = "", breaks = NULL, labels = NULL) +
    theme_void() +
    labs(title = paste0("Progress for ", sum(data$N), " Positions"),
         fill = NULL) +
    scale_fill_brewer(palette = "Blues")
Related