Home > database >  fancy pie chart in R using ggplot2
fancy pie chart in R using ggplot2

Time:05-15

I have a pie chart below, and I would like to leave extra white space between each pie and paste the value of each letter in each pie (A=25). how to get around this? many thanks in advance.

library(ggplot2)
library(dplyr)

# Create Data
data <- data.frame(
  group=LETTERS[1:5],
  value=c(13,7,9,21,2)
)

# Compute the position of labels
data <- data %>% 
  arrange(desc(group)) %>%
  mutate(prop = value / sum(data$value) *100) %>%
  mutate(ypos = cumsum(prop)- 0.5*prop )

# Basic piechart
ggplot(data, aes(x="", y=prop, fill=group))  
  geom_bar(stat="identity", width=10, color="white")  
  coord_polar("y", start=0)  
  theme_void()   
  theme(legend.position="none")  
  
  
  geom_text(aes(y = ypos, label =  round(prop,2) ), color = "white", size=4)  
  scale_fill_brewer(palette="Set1")

CodePudding user response:

You could do:

ggplot(data, aes(x="", y=prop, fill=group))  
  geom_bar(stat="identity", width=10, size = 3, color = "white")  
  coord_polar("y", start=0)  
  theme_void()   
  theme(legend.position="none")  
  geom_text(aes(y = ypos, label =  paste(group, round(prop,2), sep = "\n")), 
            color = "white", size=4, nudge_x = 3)  
  scale_fill_brewer(palette="Set1")

enter image description here

CodePudding user response:

Perhaps 3D pie chart in base R can work with explode argument set to true. e.g.

pie3D(num_data, labels = num_data, explode = 0.25)

  • Related