0
votes

I'm working now in a statistics project and recently started with R. I have some problems with the visualization. I found a lot of different tutorials about how to add percentage labels in pie charts, but after one hour of trying I still don't get it. Maybe something is different with my data frame so that this doesn't work?

It's a data frame with collected survey answers, so I'm not allowed to publish them here. The column in question (geschäftliche_lage) is a factor with three levels ("Gut", "Befriedigend", "Schlecht"). I want to add percentage labels for each level.

I used the following code in order to create the pie chart:

dataset %>%
  ggplot(aes(x= "", fill = geschäftliche_lage)) +
  geom_bar(stat= "count", width = 1, color = "white") +
  coord_polar("y", start = 0, direction = -1) +
  scale_fill_manual(values = c("#00BA38", "#619CFF", "#F8766D")) +
  theme_void()

This code gives me the desired pie chart, but without percentage labels. As soon as a I try to add percentage labels, everything is messed up. Do you know a clean code for adding percentage labels?

If you need more information or data, just let me know!

Greetings

1

1 Answers

0
votes

Using mtcars as example data. Maybe this what your are looking for:

library(ggplot2)

ggplot(mtcars, aes(x = "", fill = factor(cyl))) +
  geom_bar(stat= "count", width = 1, color = "white") +
  geom_text(aes(label = scales::percent(..count.. / sum(..count..))), stat = "count", position = position_stack(vjust = .5)) +
  coord_polar("y", start = 0, direction = -1) +
  scale_fill_manual(values = c("#00BA38", "#619CFF", "#F8766D")) +
  theme_void()

Created on 2020-05-25 by the reprex package (v0.3.0)