如何在 geom_text 中显示与 geom_bar 组变量成比例的标签

How to show labels in geom_text that is proportional to geom_bar group variable

我一直在尝试在 ggplot 中输出一个图表,该图表以百分比值和与 geom_bar 中定义的分组因子成比例的方式显示标签。我想输出一个与每个子组(在本例中为地点 A 和地点 B)成比例的标签值,而不是与总人口成比例的百分比值,但我没有设法做到。请参阅下面的可重现示例

可重现的数据帧

Random<-data.frame(replicate(3,sample(0:3,3024,rep=TRUE)))
Random$Trxn_type <- sample(c("Debit", "Credit"),
                       size = nrow(Random), 
                       prob = c(0.76, 0.24), replace = TRUE)
Random$YN <- sample(c("Yes", "No"),
                       size = nrow(Random), 
                       prob = c(0.76, 0.24), replace = TRUE)
Random$Place <- sample(c("PlaceA", "PlaceB"),
                       size = nrow(Random), 
                       prob = c(0.76, 0.24), replace = TRUE)

Random<-Random[, 4:6]

然后应用以下代码

Share<-ggplot(Random, aes(x = YN, fill=Place)) +
scale_fill_brewer(palette="Greens")+
geom_bar(aes(y = ..prop.., group = Place),position = position_dodge()) + 
facet_wrap(~ Random$Trxn_type, scales = "free_x", ncol=2)+ 
theme(strip.text.x = element_text(size = 15, colour = "black"))+
theme(panel.background = element_rect(fill = "white"),legend.position = "bottom")+
scale_y_continuous(labels = percent)+
ylab("Frequency") + 
coord_flip()+ 
xlab("Answers") + 
theme(plot.title = element_text(size = 16, face = "bold"),
      axis.text=element_text(size=12),
      axis.title=element_text(size=12))+
geom_text(aes(y=..prop..,label=scales::percent((..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])),
          stat="count", vjust=-.5, position=position_dodge(.9)) 
Share

得到如下输出

我希望看到将地点 A 和地点 B 视为两个不同群体的回复的 % 值,而不是这个百分比分布。更简单地说,我希望标签显示与直方图条的大小相对应的百分比值,方式是信用地点 A 的直方图总和为 100,信用地点 B 的直方图总和为 100。这同样适用于借记。

谢谢!

这是一个解决方案,它使用 dplyr 计算比例,然后将结果通过管道传输到 ggplot
我还将所有 theme 设置放在对 theme().
的同一次调用中 我已经重新发布了数据创建代码,这次设置RNG种子是为了使数据示例可重现。

library(dplyr)
library(ggplot2)

Random %>%
  count(Trxn_type, YN, Place) %>%
  left_join(Random %>% count(Trxn_type, name = "m"), by = "Trxn_type") %>%
  mutate(Prop = n/m) %>%
  ggplot(aes(x = YN, y = Prop, fill = Place)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label = scales::percent(Prop)),
            hjust = -0.25, 
            position = position_dodge(0.9)) +
  facet_wrap(~ Trxn_type, scales = "free_x", ncol = 2) +
  scale_fill_brewer(palette = "Greens") +
  scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
  xlab("Answers") +
  ylab("Frequency") +
  coord_flip() +
  theme(panel.background = element_rect(fill = "white"),
        legend.position = "bottom",
        strip.text.x = element_text(size = 15, colour = "black"),
        plot.title = element_text(size = 16, face = "bold"),
        axis.text = element_text(size = 12),
        axis.title = element_text(size = 12))

编辑。

根据 OP 的评论,这里有一种方法也可以按 Place 计数。 对上述代码的更改是left_join指令。

  left_join(Random %>% count(Trxn_type, Place, name = "m"),
            by = c("Trxn_type", "Place")) %>%

数据创建代码。

set.seed(1234)
Random <- data.frame(replicate(3,sample(0:3,3024,rep=TRUE)))
Random$Trxn_type <- sample(c("Debit", "Credit"),
                           size = nrow(Random),
                           prob = c(0.76, 0.24), replace = TRUE)
Random$YN <- sample(c("Yes", "No"),
                    size = nrow(Random),
                    prob = c(0.76, 0.24), replace = TRUE)
Random$Place <- sample(c("PlaceA", "PlaceB"),
                       size = nrow(Random),
                       prob = c(0.76, 0.24), replace = TRUE)

Random <- Random[, 4:6]