Seyma Kalay
Seyma Kalay

Reputation: 2859

fancy pie chart in R using ggplot2

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")

Upvotes: 0

Views: 1118

Answers (2)

Rys
Rys

Reputation: 77

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)

Upvotes: 0

Allan Cameron
Allan Cameron

Reputation: 173793

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

Upvotes: 2

Related Questions