简体   繁体   中英

Setting labels in ggplot based on value

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?

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))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM