Home > OS >  Grouped stacked bar chart in R
Grouped stacked bar chart in R

Time:09-17

I have the following dataset (in file emp1.txt and I would like to draw a grouped bar chart based on age range and also I would like to make stacked options for Male and Female for each group.

Count   Male    Female  Emp_group
38  10  28  Staff
38  20  18  Teacher
33  15  18  Teacher
34  17  17  Teacher
41  35  6   Staff
45  25  20  Teacher
35  17  18  Staff
39  30  9   Staff
39  9   30  Teacher
44  22  22  Staff
42  20  22  Teacher

This is what I have tried, but not able to figure out the stacked portion. I would appreciate any help. Both the red and green bars should be divided into two parts for Male and Female respectively. Also I would like to put color description for Male and Female in the legends.

data <- read.csv("emp1.txt", sep = "\t" , header = TRUE)
df1<-tibble(data)
df1<- mutate(df1, emp_class = cut(Count, breaks = c(0, 30, 40, 50, 60, 100), 
                                  labels = c('(0-30)', '(31-40)', '(41-50)', '(51-60)', '(61-100)'))) 

df1 <- df1 %>% 
  group_by(Emp_group) %>% 
  add_count() 

df1 <- mutate(df1, x_axis = paste(Emp_group, n, sep = "\n"))
my_ggp <- ggplot(df1, aes(x=as.factor(x_axis), fill=as.factor(emp_class))) 
  geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]*100), position="dodge")   ylab('% Employes')  xlab("")   labs(fill = "Count group") 
df1
my_ggp   theme(text = element_text(size = 20))  

enter image description here

CodePudding user response:

You need position = "stack" instead of "dodge".

I reorganised your code slightly:

library(ggplot2)
library(dplyr)

data %>% 
  mutate(emp_class = cut(Count, 
                         breaks = c(0, 30, 40, 50, 60, 100), 
                         labels = c('(0-30)', '(31-40)', '(41-50)', '(51-60)', '(61-100)')
  )
  ) %>% 
  pivot_longer(c(Male, Female),
               names_to = "MF") %>% 
  group_by(Emp_group, MF) %>% 
  add_count() %>% 
  mutate(x_axis = as.factor(paste(Emp_group, n, sep = "\n"))) %>% 
  ggplot(aes(x = x_axis, fill = as.factor(emp_class)))  
  geom_bar(aes(y = value),
           position = "fill",
           stat = "identity")   
  labs(x = "", y = "% Employes", fill = "Age group")   
  theme(text = element_text(size = 20))  
  facet_wrap(~MF)   
  scale_y_continuous(labels = scales::percent_format())

This returns

enter image description here

Data

structure(list(Count = c(38, 38, 33, 34, 41, 45, 35, 39, 39, 
44, 42), Male = c(10, 20, 15, 17, 35, 25, 17, 30, 9, 22, 20), 
    Female = c(28, 18, 18, 17, 6, 20, 18, 9, 30, 22, 22), Emp_group = c("Staff", 
    "Teacher", "Teacher", "Teacher", "Staff", "Teacher", "Staff", 
    "Staff", "Teacher", "Staff", "Teacher")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -11L), spec = structure(list(
    cols = list(Count = structure(list(), class = c("collector_double", 
    "collector")), Male = structure(list(), class = c("collector_double", 
    "collector")), Female = structure(list(), class = c("collector_double", 
    "collector")), Emp_group = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))
  • Related