-2

Here's the code:

dummy <- data.frame(
  Var1 = c("A", "B", "C", "D", "E"),
  Freq = c(548, 326, 292, 199, 138)
)

dummy %>%
  mutate(perc =scales::percent(Freq/sum(Freq))) %>%
  arrange(desc(Var1)) %>%
  mutate(pos = cumsum(Freq)- Freq/2) %>%
  ggplot(aes(x = "", y = Freq, fill = factor(Var1) )) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start = 0) +
  ggrepel::geom_label_repel(aes(x = "", y = pos, label = perc), size=5, show.legend = F, nudge_x = 1) +
  guides(fill = guide_legend(title = "Group"))

And here's the graph: enter image description here

Is there a way to make it so that instead of having a legends tab, the names of each slice is placed within the slices in the pie graph itself?

  • 1
    Possible duplicate of https://stackoverflow.com/questions/44436214/ggplot-pie-chart-labeling – akrun Sep 14 '21 at 04:18
  • add `geom_text(aes(x = "", y = pos, label = Var1))` in code. – Park Sep 14 '21 at 04:19
  • @Park is there a way to arrange the text so that they run alongside the slices? (instead of horizontally, since that makes the text overflow into other slices and overlap with other text) – Macromind101 Sep 14 '21 at 04:23

1 Answers1

1

It's pretty hard to understand what you want.

dummy %>%
  mutate(perc =scales::percent(Freq/sum(Freq))) %>%
  arrange(desc(Var1)) %>%
  mutate(pos = cumsum(Freq)- Freq/2) %>%
  ggplot(aes(x = "", y = Freq, fill = factor(Var1) )) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start = 0) +
  ggrepel::geom_label_repel(aes(x = "", y = pos, label = perc), size=5, show.legend = F, nudge_x = 1) +
  guides(fill = guide_legend(title = "Group")) +
  geom_text(aes(x=1.6, label=Var1),
           position = position_stack(vjust=0.5))

enter image description here

Park
  • 14,771
  • 6
  • 10
  • 29
  • I'm asking how to make the labels "A", "B", "C", "D", "E" rotated so that they are running alongside the same direction each slice is pointing at. Because my actual code (which I don't want to post here) has labels that are not just a single letter. The way my code is right now, the labels for each slice are overlapping with each other because they are all horizontal. – Macromind101 Sep 14 '21 at 04:40
  • @Macromind101 As I said in your previous question, since you didn't provide a reproducible example, it's very hard to answer your question. – Park Sep 14 '21 at 04:44
  • is there a way for me to PM you? Since again, I don't want to make my code public – Macromind101 Sep 14 '21 at 04:48
  • @Macromind101 Please follow [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and provide an example data, and precise problem. – Park Sep 14 '21 at 04:50