首页 > 解决方案 > 带有组和构面的堆叠 ggplot 条形图的百分比标签

问题描述

我正在尝试将百分比标签添加到堆叠和多面条形图(位置='填充')。我希望显示的百分比为每个条形加起来。

我正在使用这样的数据集:

## recreate dataset
Village<-c(rep('Vil1',10),rep('Vil2',10))
livestock<-c('p','p','p','c','c','s','s','s','g','g',
             'p','p','c','c','s','s','s','s','g','g')
dose<-c(3,2,1,2,1,3,2,1,2,1,
        2,1,2,1,4,3,2,1,2,1)
Freq<-c(4,5,5,2,3,4,1,1,6,8,
      1,3,2,2,1,1,3,2,1,1)
df<-data.frame(Village,livestock,dose,Freq)

我成功地绘制了它并添加了标签,每个 X 变量(牲畜)的总和为 100%:

## create dose categories (factors)
df$dose<-as.character(df$dose)
df$dose[as.numeric(df$dose)>3]<-'>3'
df$dose<-factor(df$dose,levels=c('1','2','3','>3'))
## percentage barplot
ggplot(data = df, aes(x=livestock, y=Freq, fill=dose)) +
  geom_bar(position='fill', stat='identity') +
  labs(title="Given doses of different drugs in last 6months (livestock)", 
       subtitle='n=89',x="Livestock",y="Percentage",
       fill = "Nr. of\ndoses") +
  theme(axis.text.x = element_text(angle = 45, hjust=1))+ 
  scale_y_continuous(labels=percent)+
  facet_wrap(~Village)+
  geom_text(aes(label = percent(..y../tapply(..y..,..x..,sum)[..x..])),
            stat = "identity",position = position_fill(vjust=0.5))

每个 Xvariable 的条形图 100%

有谁知道我可以如何更改 ggplot 中的标签代码,以便每个条形的百分比加起来为 100%?也许与..group..有关?

我尝试了类似的方法:ggplot2 中的多面填充条形图中的标签百分比放置我无法使其适用于我的数据。

标签: rggplot2bar-chartgeom-bargeom-text

解决方案


只是为了添加@teunbrand 的解决方案:我按照@teunbrand 的建议计算了分数,并且效果很好。但是,我开始收到非常奇怪且持久的警告消息:

Warning messages:
1: Unknown or uninitialised column: `times`. 
2: Unknown or uninitialised column: `times`. 
3: Unknown or uninitialised column: `times`. 
4: Unknown or uninitialised column: `times`. 
5: Unknown or uninitialised column: `Var1`. 

我在这里阅读了这个问题,这似乎是一个已知的错误:Persistent "Unknown or uninitialised column" warnings

我可以通过取消分组并将小标题重新转换为数据框来消除警告。

df <- as.data.frame(df %>% group_by(Village, livestock) %>%
  mutate(frac = Freq / sum(Freq)) %>% ungroup())

推荐阅读