繁体   English   中英

Ggplot2:饼图标签中不显示 0 值

[英]Ggplot2 : do not display 0 value in pie chart labels

我正在尝试制作饼图而不显示 0 值 label,但我无法做到。

我的数据:

group <- factor(c("A","B","C","A","B","C","A","B","C","A","B","C"))
prod <-factor(c("Fong","Fong","Fong","Herb","Herb","Herb","Ins","Ins","Ins","Other","Other","Other"))
quant <- c(0,1,0,2,1,1,0,5,3,8,4,6)

df <- data.frame(group, prod, quant)

我的脚本:

library(ggplot2)
ggplot(df, aes(x="", y=quant, fill=prod))+
  geom_col()+
  coord_polar(theta = "y", start=0)+
  facet_wrap(~group)+
  geom_text(aes(label=quant), position=position_stack(vjust=0.5))

这是结果:饼图

有没有不显示 0 值标签的解决方案?

您可以用 NA 替换0值,例如 dataframe 上的dplyr::na_if(0)


df %>% 
  dplyr::na_if(0) %>% 
ggplot(aes(x="", y=quant, fill=prod))+
  geom_col()+
  coord_polar(theta = "y", start=0)+
  facet_wrap(~group)+
  geom_text(aes(label=quant), position=position_stack(vjust=0.5))

暂无
暂无

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

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