How to draw a pie chart with labels close to circumference ?

  data <- data.frame(Category = c("Baseball",
                                  "Basketball",
                                  "Football",
                                  "Hockey"),
                     n = c(45,10,25,20)) 
  
  # create pie
  library(ggplot2)
  pie <-   ggplot(data, aes (x="", y = n, fill = Category)) +
    geom_bar(width = 1, stat = "identity") +
    coord_polar("y", start=0)
  
  # attempt 1, labels too close to circle center (midway to circumference)
  pie + geom_text(aes(label = paste(n, "%")),
                  position = position_stack(vjust = 0.5)) 
  
  # attempt 2 , wrong (reverse) order of labels 
  pie + geom_text(aes(y = n/2 + c(0, cumsum(n)[-length(n)]), 
                      x =1.25,
                      label= paste0(n, "%")))  

go with the first attempt:

pie + geom_text(aes(label = paste(n, "%"),x=1.25),
                position = position_stack(vjust = 0.5))
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.