繁体   English   中英

根据值在ggplot中设置标签

[英]Setting labels in ggplot based on value

我正在制作一个带有百分比标签的 ggplot(饼图)。 我想设置如果百分比小于 1%,则 label 将为“<1%”。 有没有办法设置这个?

正如@teunbrand 所建议的那样。 此处 label < 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))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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