Reputation: 1
I'm making a ggplot (pie chart) where I have percentage labels. I want to set that if the percentage is less than 1%, the label will be "<1%". Is there any way to set this?
Upvotes: 0
Views: 49
Reputation: 86
As suggested by @teunbrand. Here the label is < 10%.
library(dplyr)
library(ggplot2)
data <- data.frame(a=c("a","a","a","a","a","a",
"b",
"c","c","c","c","c","c"),
b=1:13)
data <- data %>%
group_by(a) %>%
count() %>%
ungroup() %>%
mutate(per=`n`/sum(`n`))
ggplot(data=data)+
geom_bar(aes(x="", y=per, fill=a),
stat="identity",
width = 1)+
coord_polar("y", start=0)+
theme_void()+
geom_text(aes(x=1, y = cumsum(per) - per/2,
label=ifelse(per < 0.1, "<10%",scales::percent(per))))
Upvotes: 2