Home > other >  Barplot with ggplot2 (R), one variable and one modality from another variable
Barplot with ggplot2 (R), one variable and one modality from another variable

Time:01-17

I can't find a solution to what seems to me to be a small problem: do you know how to make a bar plot with ggplot2 that links a variable (in this case age classes) with a modality of another variable ?

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", "non stable", "stable", "stable", "stable", "non     stable", "non stable", "stable", "stable")

df <- data.frame(age10,relation)

df1 <- 
  df %>%
  group_by(age10, relation) %>%
  summarise(n = n()) 

ggplot(df1)  
  aes(fill = relation, y = n, x = age10)  
  geom_bar(position = "fill", stat = "identity")  
  scale_y_continuous(label = percent)  
  labs(x = "Age group", y = "Percentage", 
       title = "relational status by age group")``

the barplot gives me the percentage of stable and non stable relationships stacked, whereas I'd like it to show only the percentage of non stable relationships (or stable) for each age group.

CodePudding user response:

One option would be to compute the percentages manually, then you could use e.g. facet_wrap to show the percentages separately for each relation:

df1 <-
  df %>%
  group_by(age10, relation) %>%
  summarise(n = n()) %>% 
  mutate(pct = n / sum(n), relation = str_squish(relation))

ggplot(df1)  
  aes(fill = relation, y = pct, x = age10)  
  geom_bar(stat = "identity")  
  scale_y_continuous(label = percent)  
  facet_wrap(~relation)  
  labs(
    x = "Age group", y = "Percentage",
    title = "relational status by age group"
  )

enter image description here

EDIT And if you want oto display only one category filter your dataset and of course remove the facet_wrap:

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

ggplot(df1)  
  aes(fill = relation, y = pct, x = age10)  
  geom_bar(stat = "identity")  
  scale_y_continuous(label = percent)  
  labs(
    x = "Age group", y = "Percentage",
    title = "relational status by age group"
  )

enter image description here

  • Related