r - Ggplot2:不在饼图标签中显示 0 值
问题描述
我正在尝试制作饼图而不显示 0 值标签,但我无法做到。
我的数据:
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
值,例如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))
推荐阅读
- php - Laravel - 防止多余的字符 slug 参数
- java - 创建新的android studio项目时出错
- python - 如何将索引彩色图像 numpy 数组转换为 RGB 颜色空间?
- .net - 当应用程序关闭时,当事件处理程序是异步的时,UWP 不会结束调试
- internet-explorer - 我可以在 ActiveX 中使用 IHTMLDocument2 访问 HttpOnly cookie 吗?
- javascript - 在 JS (ES6) 中修改 post 请求的数据
- tensorflow - 为循环神经网络 RNN 定义标签
- vertical-alignment - Clarity Design Systems - 垂直导航
- dart - Dart/flutter:List的单个元素
当函数需要字符串输入时导致异常 - python - 尽管有它们,但烧瓶 sqlalchemy 仍出现未知列错误