【问题标题】:Grouped stacked bar chart in RR中的分组堆积条形图
【发布时间】:2021-09-09 17:59:48
【问题描述】:

我有以下数据集(在文件 emp1.txt 中,我想根据年龄范围绘制一个分组条形图,并且我想为每个组创建 MaleFemale 的堆叠选项。

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

【问题讨论】:

  • 这不是一个可重现的例子。我们无权访问"emp1.txt"。请使用dput()将其放入您的问题中。
  • 谢谢!!我已经更新了这个问题。 emp1.txt 是一个大文件,所以我只是复制了前几行并放在这里。

标签: r ggplot2 stacked-chart


【解决方案1】:

您需要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())

返回

数据

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

【讨论】:

  • 谢谢!!我已经试过了。实际上,我想为两个不同的组设置两个用于 SStaffTeacher 的条,但每个条都应该垂直拆分以用于 MaleFemale 计数。这有意义吗?
  • 制作了一个新版本。也许这就是您要找的东西?
  • 是的,看起来不错。但似乎FemaleMale 完全相同
  • @BeginneR 最后一次尝试... ;-)