With the following code
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")
I get this plot: image of plot
Here the woman and man are put on top of each other for each marital status. However I would like to have the bars for men and women next to each other for each marital status. So instead of having for married one bar with male and female I would like to have two bars for married, one for each gender. I have not found out how to do this. Maybe someone can help?
Maybe the table of the values is helpful: table
To give you an idea how the results looks like, I created random numbers with for your data:
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))
Random data:
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
You should set position_dodge()
in your geom_col
function to create space between your bars as like @Basti also mentioned. You can use the following code:
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")
Output:
As you can see the bars are now nicely displayed. (Don't look at the numbers, they are fake)