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

尼古拉

我一直试图在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。借记卡也是如此。

谢谢!

瑞·巴拉达斯(Rui Barradas)

这是一种解决方案,可以计算的比例,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]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 geom_bar 的图例中只显示 3 个标签中的 2 个

ggplot的饼图的geom_text()标签隐藏了geom_bar()制作的饼

R:用于合并的geom_bar的ggplot2 geom_text数据标签

Geom_bar与带有两个离散变量的geom_text的比较

如何在ggplot的geom_text_repel或geom_text标签中包含斜体文本?

geom_text不标记躲避的geom_bar

ggplot2:geom_text调整大小,并在geom_bar中强制/拟合文本

ggplot geom_bar的比例

如何在ggplot的geom_text标签中键入下标

如何在ggplot2中的geom_segment箭头之后将geom_text标签移动?

如何在geom_text中为多行创建唯一标签?

如何在geom_text的文本标签中添加逗号分隔符?

如何在ggplot geom_text标签中添加百分比和分数?

如何在ggplot2的geom_text中舍入条形图的数据标签?

删除 geom_text 中的重复标签

如何在躲避的geom_col上方对齐geom_text标签

如何在ggplot2中将geom_text显示为百万

geom_bar重叠标签

R中的Geom_text()-如何更改geom_point中特定点的标签位置

如何在R中均匀绘制geom_text?

如何在 geom_Text 中给出多个条件

geom_bar与geom_text位置有关的问题

如何使用geom_bar中的aes(group = x)计算比例?

订购变量geom_bar

如何在 ggplot2 中的 geom_bar() 上放置标签(没有手动 y_position)

如何在具有ggplot的R中的多面堆叠条形图中使用不同的geom_text()标签?

R:更改geom_bar中的条形标签

如何显示选定的图例但在堆叠的 geom_bar 中显示所有颜色

如果bar的名称类别是字符,如何使用geom_bar连接堆叠的bar比例