Home > Mobile >  How to had a legend to a barplot with two variables and two data set?
How to had a legend to a barplot with two variables and two data set?

Time:01-17

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 : enter image description here

  • Related