Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

legend

ggplot2

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.

like image 544
lpambout Avatar asked Dec 05 '25 01:12

lpambout


1 Answers

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

like image 162
stefan Avatar answered Dec 06 '25 17:12

stefan