【问题标题】:Ggplot2 : do not display 0 value in pie chart labelsGgplot2:不在饼图标签中显示 0 值
【发布时间】:2021-06-10 11:19:40
【问题描述】:

我正在尝试制作一个不显示 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))

这里是结果: Pie chart

有什么办法可以不显示0值标签吗?

【问题讨论】:

    标签: r pie-chart


    【解决方案1】:

    您可以用 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多