Pie chart labels for small pieces (ggplot)

I want to make a pie chart in ggplot

My details:

lab <- c("a", "b", "c", "d", "e", "f", "g", "h")
percentage <- c(50, 20, 10, 10, 2, 2,2,2)
df.prison <- data.frame(lab, percentage)
df.prison$crime <- factor(df.prison$lab, levels=rev(levels(df.prison$lab)))
labels.prison <- paste(lab, "-", percentage, "%", sep="")

Plot:

plot <- ggplot(data=df.prison, aes(x=factor(1), y=percentage, fill=factor(lab))) +
   geom_bar(width=1, stat="identity") +
   coord_polar(theta="y") +
   ylab("") +
   xlab("") +
   labs(fill="") +
   theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.text = element_blank()) +
   geom_text(aes(y = percentage/2 + c(0, cumsum(percentage)[-length(percentage)]), label=labels.prison))
plot

enter image description here

I have two problems with this plot: 1. I don’t want to have a legend (because the labels are very short (one letter), and I want to have them on the pie chart 2. Is it possible to place labels for small pieces (less than a few percent) next to plot because the label is too big to fit inside this small piece. For example, for example:

http://www.conceptdraw.com/How-To-Guide/picture/Pie-chart-Sector-weightings.png

Thanks for the consultation:)

+4
source share
1 answer

legend.position = 'none' . , x geom_text.

library(ggplot2)

lab <- c("a", "b", "c", "d", "e", "f", "g", "h")
percentage <- c(50, 20, 10, 10, 2, 2, 2, 2)
df.prison <- data.frame(lab, percentage)
df.prison$crime <- factor(df.prison$lab, levels=rev(levels(df.prison$lab)))
labels.prison <- paste(lab, "-", percentage, "%", sep="")

ggplot(data=df.prison, aes(x=factor(1), y=percentage, fill=factor(lab))) +
   geom_bar(width=1, stat="identity") +
   coord_polar(theta="y") +
   ylab("") +
   xlab("") +
   labs(fill="") +
   theme(legend.position = "none",   ### Solution to part 1, no legend
         axis.ticks = element_blank(), 
         panel.grid = element_blank(), 
         axis.text  = element_blank()) +
   geom_text(aes(x = c(1, 1, 1, 1, 1.2, 1.3, 1.4, 1.5), # Solution for part 2,
                 y = percentage / 2 + c(0, cumsum(percentage)[-length(percentage)]), 
                 label=labels.prison))

enter image description here

+2

All Articles