geom_col in R使条彼此相邻

2q5ifsrm  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(94)

使用以下代码

A2c%>%
  group_by (maritalStatus, Geschlecht)%>%
  summarise (nbr_total=n(), nbr_adipos=sum(Adipös))%>%
  mutate( adipos_prozent = 100 * nbr_adipos / nbr_total )%>%
  filter (maritalStatus != "Refused")%>%
  ggplot+
  geom_col(aes(x=maritalStatus, y= adipos_prozent, color = Geschlecht, fill=Geschlecht))+
  theme(axis.text.x = element_text(angle = 90))+
  labs(y = "% Adipöser", x = "Marital Status")

我得到了这个图:image of plot
这里的女人和男人是放在对方的顶部为每一个婚姻状况。然而,我想有酒吧的男子和妇女旁边的对方为每一个婚姻状况。所以,而不是为已婚一个酒吧与男性和女性,我想有两个酒吧已婚,一个为每一个性别。我还没有找到如何做到这一点。也许有人可以帮助?
可能数值表会有所帮助:table

nqwrtyyt

nqwrtyyt1#

为了让您了解结果的样子,我为您的数据创建了随机数:

A2c <- data.frame(maritalStatus = c("Married", "Married", "Widowed", "Widowed", "Divorced", "Divorced", "Separated", "Separated", "Never married", "Never married"),
                  Geschlecht = c("Männlich", "Weiblich", "Männlich", "Weiblich", "Männlich", "Weiblich", "Männlich","Weiblich", "Männlich", "Weiblich"),
                  nbr_total = sample(1:1500, 10),
                  Adipös = sample(1:600, 10))

随机数据:

maritalStatus Geschlecht nbr_total Adipös
1        Married   Männlich       376    422
2        Married   Weiblich        33    261
3        Widowed   Männlich       989    458
4        Widowed   Weiblich       329    397
5       Divorced   Männlich        41    222
6       Divorced   Weiblich       741    283
7      Separated   Männlich       743    579
8      Separated   Weiblich       236    262
9  Never married   Männlich        85     25
10 Never married   Weiblich       402    145

你应该在geom_col函数中设置position_dodge()来在你的条之间创建空间,就像前面提到的@Basti一样。

library(tidyverse)
A2c%>%
  group_by (maritalStatus, Geschlecht)%>%
  summarise (nbr_total=n(), nbr_adipos=sum(Adipös))%>%
  mutate( adipos_prozent = 100 * nbr_adipos / nbr_total )%>%
  filter (maritalStatus != "Refused")%>%
  ggplot +
  geom_col(aes(x=maritalStatus, y= adipos_prozent, color = Geschlecht, fill=Geschlecht), position = position_dodge(1))+
  theme(axis.text.x = element_text(angle = 90))+
  labs(y = "% Adipöser", x = "Marital Status")

输出:

正如你所看到的,现在的酒吧很好地显示。(不要看数字,他们是假的)

相关问题