R中的分组堆积条形图

Grouped stacked bar chart in R

我有以下数据集(在文件 emp1.txt 中,我想根据年龄范围绘制一个分组条形图,我还想为 Male 和 [=每组 14=]。

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

这是我试过的方法,但无法找出堆叠部分。我将不胜感激任何帮助。红色和绿色条应分别分为 MaleFemale 两部分。另外,我想在图例中添加 MaleFemale 的颜色描述。

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))  

您需要 position = "stack" 而不是 "dodge"

我稍微重组了你的代码:

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())

这个returns

数据

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"))