0

I am plotting pie charts with ggplot2 and succeeded in having the percentage-labels centered in each slice

library(dplyr) 
library(ggplot2)
library(ggpubr)
library("readxl")
df <- read_excel("Radiocomp.xlsx")

df$Pattern <- factor(cc$Pattern)
str(cc)

GGO <- ggplot(data=df, aes(x = "", y = GGO, fill = Pattern)) +
  geom_bar(stat="identity", color = "white") +
  geom_text(aes(label = paste0(GGO, "%")), position = position_stack(vjust = 0.5)) +
  coord_polar("y") +
  theme_void()

GGO

Pie chart

I try to place the percent-label outside the pie for better readability

Any recommendation?

Thank you

1 Answer 1

5

This can be achieved by setting the x aesthetic inside geom_text, e.g. x = 1.6 will put the label just outside of the pie.

library(ggplot2)
library(dplyr)

# example data
mpg1 <- mpg %>% 
  count(class) %>% 
  mutate(pct = n / sum(n))

ggplot(mpg1, aes(x = "", y = pct, fill = class)) +
  geom_bar(stat = "identity", color = "white") +
  geom_text(aes(x = 1.6, label = scales::percent(pct, accuracy = .1)), position = position_stack(vjust = .5)) +
  coord_polar("y") +
  theme_void()

Created on 2020-06-03 by the reprex package (v0.3.0)

2
  • Thank you very much for your valuable feedback. Adding x aesthetic inside geom_text almost worked, however x values below 2 cause donuts and displaced labels and x values above 2 place the labels too far outside the chart.
    – xray_mash
    Jun 4, 2020 at 6:42
  • Hi @xray_mash. That's the way it is. (; But to the best of my knowledge this is by far the simplest solution. For a lengthy discussion on drawing pie charts and setting labels see e.g. stackoverflow.com/questions/16184188/…. BTW: Positioning labels always requires some manual work and tweaking.
    – stefan
    Jun 4, 2020 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.