3

I've made two bar plot (one for women, one for men that represent my two data set) for each age groupe (variable 1) regarding their relational status (variable 2). However, i can't ad a simply legend whose title would be "Sex" and which, for each colour of the two bar plots, would give the corresponding sex modality ("man" and "woman")

Here is my code :

library(tidyverse)
library(scales)

age10 <- c("18-34","35-54", "55+", "55+","35-54","18-34","18-34","35-54","35-54","35-54")
relation <- c("stable","non stable", "stable", "stable", "stable", "stable", "non stable", "non stable", "stable", "stable")
sexe <- c("woman", "woman", "man", "man", "woman", "man", "woman", "man", "woman", "woman")

df <- data.frame(age10,relation,sexe)

df_woman <-
  df %>%
  filter(sexe != "man")

dff_woman <-  
  df_woman %>%
  group_by(age10, relation) %>%
  summarise(n = n())  %>% 
  mutate(pct = n / sum(n), relation = str_squish(relation)) %>% 
  filter(relation == "stable")


df_man <- 
  df %>%
  filter(sexe != "woman")      

dff_man <-  
  df_man %>%
  group_by(age10, relation) %>%
  summarise(n = n())  %>% 
  mutate(pct = n / sum(n), relation = str_squish(relation)) %>% 
  filter(relation == "stable")


ggplot() +
  geom_bar(data = dff_man, aes(fill = relation, y = pct, x = age10, color = "man"), 
           stat = "identity", fill = "#99d8c9", width = 0.4, position=position_nudge(x = 0.2)) +
  geom_bar(data = dff_woman, aes(fill = relation, y = pct, x = age10, color = "woman"),
           stat = "identity", fill = "#bcbddc", width = 0.4, position=position_nudge(x = -0.2)) +
  scale_y_continuous(label = percent) +
  facet_wrap(~relation) +
  labs(
    x = "Age group", y = "Percentage") +
  expand_limits(y = 1) +
  theme(text = element_text(family = "Times New Roman"))


Here is the resulting plot I don't want the outline of my bars to be coloured, but the caption should refer to the colour of the bars I've search for answer here : Adding legends to multiple line plots with ggplot, and here : How to annotate a bar plot and add a custom legend, but I don't find answer.

akrun
  • 874,273
  • 37
  • 540
  • 662
lpambout
  • 59
  • 4

1 Answers1

3

You could get your results much easier using just one dataframe, by mapping sexe on fill instead of on color and by setting your desired colors via scale_fill_manual:

library(tidyverse)
library(scales)

df <- df %>%
  group_by(age10, sexe, relation) %>%
  summarise(n = n()) %>%
  mutate(pct = n / sum(n)) %>%
  ungroup() |>
  filter(relation == "stable")

ggplot(data = df) +
  geom_col(aes(fill = sexe, y = pct, x = age10),
    width = 0.75, position = position_dodge(preserve = "single")
  ) +
  scale_y_continuous(label = percent) +
  scale_fill_manual(values = c(man = "#99d8c9", woman = "#bcbddc")) +
  facet_wrap(~relation) +
  labs(
    x = "Age group", y = "Percentage"
  ) +
  expand_limits(y = 1) +
  theme(text = element_text(family = "Times New Roman"))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Very nice. Thanks for `position_dodge(preserve = "single")`. Did you know you can do `group_by(across(,))` for `group_by(age10, sexe, relation)`. – TarJae Jan 16 '23 at 22:33
  • 1
    Thx @TarJae. No. Didn't knew this shortcut. Thanks for sharing. – stefan Jan 16 '23 at 23:04