Home > Back-end >  How do I overlay a bar charts in ggplot2?
How do I overlay a bar charts in ggplot2?

Time:02-26

I would like to create a bar chart that displays what percentage worked within the agricultural sector, by gender, throughout the years.

My dataframe looks like this:

agri_work <- c(0, 1)
Male_ratio_02 <- c(25.72, 23.13)
Female_ratio_02 <- c(25.77, 26.58) 
Male_ratio_04 <- c(28.78, 20.36)
Female_ratio_04 <- c(27.92, 22.95) 

agrisum <- data.frame(agri_work, Male_ratio_02, Female_ratio_02, Male_ratio_04, Female_ratio_04) 

I have tried the following code:

ggplot(agrisum)   
  geom_col(aes(x = Male_ratio_02, y = agri_work, fill = agri_work), stat = "identity")  
  geom_col(aes(x = Female_ratio_02, y = agri_work, fill = agri_work), stat = "identity")

enter image description here

However, the graph I get does not tell me what female/male population worked in agriculture.

CodePudding user response:

At the moment your data is in wide format. It is going to be difficult to plot it without reshaping.

Your current data looks like this:

agrisum
#>   agri_work Male_ratio_02 Female_ratio_02 Male_ratio_04 Female_ratio_04
#> 1         0         25.72           25.77         28.78           27.92
#> 2         1         23.13           26.58         20.36           22.95

In order to plot this easily, we should have one observation per row, and one variable per column. At the moment, the gender and year are distributed between columns. We can fix this using dplyr and tidyr from the tidyverse:

library(dplyr)
library(tidyr)

agrisum <- agrisum %>%
  pivot_longer(2:5) %>%
  separate(name, sep = "_", into = c("Gender", NA, "Year")) %>%
  mutate(Work = c("Agricultural", "Non-Agricultural")[agri_work   1])

agrisum
#> # A tibble: 8 x 5
#>   agri_work Gender Year  value Work            
#>       <dbl> <chr>  <chr> <dbl> <chr>           
#> 1         0 Male   02     25.7 Agricultural    
#> 2         0 Female 02     25.8 Agricultural    
#> 3         0 Male   04     28.8 Agricultural    
#> 4         0 Female 04     27.9 Agricultural    
#> 5         1 Male   02     23.1 Non-Agricultural
#> 6         1 Female 02     26.6 Non-Agricultural
#> 7         1 Male   04     20.4 Non-Agricultural
#> 8         1 Female 04     23.0 Non-Agricultural

So now we can do:

ggplot(agrisum, aes(Work, value, fill = Gender))  
  geom_col(position = position_dodge(width = 0.8), width = 0.6, 
           color = "black", size = 0.1)  
  scale_fill_manual(values = c(Female = "orange", Male = "deepskyblue3"))  
  facet_grid(. ~ paste0("20", Year))  
  theme_bw()  
  theme(strip.background = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line())

enter image description here

  • Related