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

问题描述 投票:1回答:1

我一直试图在ggplot中输出一个图形,该图形以百分比值和与geom_bar中定义的分组因子成比例的方式显示标签。我不想输出与总人口成比例的%值,而是输出与每个子组成比例的标签值(在本例中为Place A和Place 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

并获得以下输出

enter image description here

我希望将位置A和位置B视为两个单独的总体,而不是百分比分布。简而言之,我希望标签以直方图条形图的大小显示%值,以使贷方A的直方图总计为100,贷方B的直方图总计为100。借记卡也是如此。

谢谢!

r ggplot2 label geom-bar geom-text
1个回答
0
投票

这里是一个用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))

enter image description here

编辑。

[根据OP的评论,这也是一种以Place进行计数的方法。对以上代码的only更改是left_join指令。

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

enter image description here

数据创建代码。

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]
© www.soinside.com 2019 - 2024. All rights reserved.