1

I have data like this and I need to draw the pie chart:

# Creat bar chart for jam language
library("ggplot2")  # Data visualization
library("dplyr")    # Data manipulation
library("RColorBrewer")
myPalette <- brewer.pal(5, "Set2") 


all_languages <- data.frame(
  Languages = c("English", "French", "Spanish", "Portuguese", "Others"),
  n = c(81, 4, 3, 2, 3),
  prop = c(87.1, 4.3, 3.2, 2.2, 3.2)
)

# Add label position
all_languages  <-all_languages %>%
  arrange(desc(Languages)) %>%
  mutate(lab.ypos = cumsum(n) - 0.5*n)


ggplot(all_languages , aes(x = "", y = n, fill = Languages)) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0)+
  geom_text(aes(y = lab.ypos, label = n), color = "white")+
  scale_fill_manual(values = myPalette) +
  theme_void()

Here is my chart: enter image description here

Now I want to also add the percentage outside the chart at the corresponding slice. So my English slide will have 87.1% outside its circumference. My percentage is stored in all_languages$prop. How do I do this?

Secondly, how do I move my label just a little bit farther from the center? This is because in the smaller slices, the number labels are too cluttered together.

ATTEMPT: I did try to follow this answer How can I put the labels outside of piechart?

But it throws an error that says 2 rows are missing... Particularly, my Spanish and Portuguese slices don't have the label. Also, my case is different because I want BOTH label inside and percentage outside.

I tried to follow that answer with the following codes:

all_languages <-all_languages %>%
  arrange(desc(Languages)) %>%
  mutate(end = 2 * pi * cumsum(prop)/sum(prop),
       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))

ggplot(all_languages) + 
  geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
                   start = start, end = end, fill = Languages)) +
  geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = prop,
                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, 1),      # Adjust so labels are not cut off
                     name = "", breaks = NULL, labels = NULL)

I got the below warning

Warning message: Removed 2 rows containing missing values (geom_text)

and my pie is missing label for 2 slices: enter image description here

Final decision: I will follow Bappa Das's answer below, and edit the labels a bit. I will put both the count and the percentage outside the pie.

hydradon
  • 1,316
  • 1
  • 21
  • 52
  • Does this answer your question? [How can I put the labels outside of piechart?](https://stackoverflow.com/questions/48184645/how-can-i-put-the-labels-outside-of-piechart) – camille Nov 07 '19 at 16:24
  • Hi, I did try that, but it throws an error that says 2 rows are missing... Particularly, my Spanish and Portuguese slices don't have the label. Also, my case is different because I want `BOTH` label inside and percentage outside – hydradon Nov 07 '19 at 16:26
  • The fact that you wanted 2 separate labels with different positioning wasn't clear initially. I'm not sure what you're trying that's getting you an error—if that's a specific thing you need help debugging, you can add it to the question. Instead of setting `x = ""`, use a dummy numeric value so you can set a position for the slices (try x = 0) and a position for the outside text relative to that (try x = 0.6). – camille Nov 07 '19 at 16:41
  • HI, I have updated with the exact code I used to follow that answer. And I got the warning with missing row. The code can be replicated as is. – hydradon Nov 07 '19 at 17:07
  • Cool, now that you've made it more clear what you're trying to do, I'm retracting my close vote – camille Nov 07 '19 at 17:10

1 Answers1

2

You can use the following code with base R

library(RColorBrewer)
Languages = c("English", "French", "Spanish", "Portuguese", "Others")
n = c(81, 4, 3, 2, 3)
prop = c("87.1", "4.3", "3.2", "2.2", "3.2")

lbls <- paste(prop,"%",sep=" ")
lgd <- c("English", "French", "Spanish", "Portuguese", "Others")
cols = brewer.pal(n = length(prop), name = 'Set3')
pie(n, labels = lbls, col=cols)
legend("topright", legend=lgd, cex=0.9, bty = "n", fill = cols)

enter image description here

Is this what you want?

Community
  • 1
  • 1
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Hi, your code throws an error at the `pie` function: `Error in pie(prop, labels = lbls, col = cols) : 'x' values must be positive.` – hydradon Nov 07 '19 at 17:18