geom_col 在 R 中使条形彼此相邻
geom_col in R making bars next to each other
用下面的代码
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 有帮助:table
为了让您了解结果如何,我为您的数据创建了随机数:
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()
以在您的柱之间创建 space 就像@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")
输出:
如您所见,现在可以很好地显示条形图。 (别看数字,都是假的)
用下面的代码
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 有帮助:table
为了让您了解结果如何,我为您的数据创建了随机数:
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()
以在您的柱之间创建 space 就像@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")
输出:
如您所见,现在可以很好地显示条形图。 (别看数字,都是假的)