五个尺度和四个类别的堆积条

stacked bar for five scales and four categories

我需要在质押柱上展示我的数据。五个量表共四个类别,样本量为200

df <- tribble(
    ~ID, ~scale1,  ~scale2, ~scale3, ~scale4, ~scale5,
    "1",  "Cat1",    "Cat2",    "Cat4",   "Cat1",  "Cat2",
    "2",  "Cat3",    "Cat3",    "Cat1",   "Cat2",  "Cat3",
    "3",  "Cat4",    "Cat1",    "Cat2",   "Cat3",  "Cat2",
    "4",  "Cat1",    "Cat4",    "Cat2",   "Cat4",  "Cat4",
    "5",  "Cat2",    "Cat1",    "Cat3",   "Cat1",  "Cat1")

如何为每个尺度创建堆积条? 我试过了,但没用

df=df %>%
  select(ID,scale1:scale5) %>%
  pivot_longer(cols=scale1:scale5,names_to="Scale",values_to = "Value")%>%
  mutate(Category=case_when(
    Value>=4 ~"Cat4",
    Value>=3 &Value<4 ~"Cat3",
    Value>=2 &Value<3 ~"Cat2",
    Value<2 ~"Cat1",
    TRUE ~ "None"
  )) %>%
  mutate(Category=factor(Category,levels = c("Cat4","Cat3","Cat2","Cat1")))%>%
  mutate(Scale=factor(Scale, levels =c("Math","Physics","","Computer","PE","English")))

这是您要找的吗?我更改了您代码的最后一部分以包含另一个 mutate + case_when 然后我将您的填充更改为 Value.

这有点令人困惑,因为我认为您的第一个 mutate + case_when 没有做任何事情,而且您共享的数据框中只有五个尺度,因此英语没有出现在任何地方。

df <- tribble(
    ~ID, ~scale1,  ~scale2, ~scale3, ~scale4, ~scale5,
    "1",  "Cat1",    "Cat2",    "Cat4",   "Cat1",  "Cat2",
    "2",  "Cat3",    "Cat3",    "Cat1",   "Cat2",  "Cat3",
    "3",  "Cat4",    "Cat1",    "Cat2",   "Cat3",  "Cat2",
    "4",  "Cat1",    "Cat4",    "Cat2",   "Cat4",  "Cat4",
    "5",  "Cat2",    "Cat1",    "Cat3",   "Cat1",  "Cat1")


df=df %>%
  select(ID,scale1:scale5) %>%
  pivot_longer(cols=scale1:scale5,names_to="Scale",values_to = "Value")%>%
  mutate(Category=case_when(
    Value>=4 ~"Cat4",
    Value>=3 &Value<4 ~"Cat3",
    Value>=2 &Value<3 ~"Cat2",
    Value<2 ~"Cat1",
    TRUE ~ "None"
  )) %>%
  mutate(Category=factor(Category,levels = c("Cat4","Cat3","Cat2","Cat1"))) %>%
    mutate(Scale=case_when(
    Scale=="scale1" ~"Math",
    Scale=="scale2" ~"Physics",
    Scale=="scale3" ~"",
    Scale=="scale4" ~"Computer",
    Scale=="scale5" ~"PE",
    Scale=="scale6" ~"English"))
    

ggplot(na.omit(df), aes(fill=Value, x=Scale)) + 
  geom_bar(stat="count", position = 'stack')+
  theme_bw()

OP 评论更新: 我现在将展示如何绘制带有计数和百分比的堆栈条。最重要的是整齐的数据。也就是说,您应该了解如何在绘图之前整理数据。 这是您的示例完整解决方案:

library(tidyverse)

# rename column names with vector
colnames(df) <- c("ID", "Math", "Physics", "_", "Computer","PE","English")


df1 <- df %>% 
    pivot_longer(
        cols = 2:6,
        names_to = "Scale",
        values_to = "Category"
    ) %>% 
    group_by(Scale, Category) %>% 
    count(Category) %>% 
    group_by(Scale) %>% 
    mutate(percent=n/sum(n)) %>% 
    print(n=40)


# plot count
ggplot(df1, aes(fill=Category, y=n, x=Scale)) + 
    geom_bar(position="stack", stat="identity") +
    geom_text(aes(label=paste0(n)),
              position=position_stack(vjust=0.5), colour="white", size = 5)


# plot with percent
ggplot(df1, aes(fill=Category, y=n, x=Scale)) + 
    geom_bar(position="fill", stat="identity") +
    geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")),
              position=position_fill(vjust=0.5), colour="white", size = 5) +
    scale_y_continuous(labels = scales::percent)

第一个回答: 您可以通过以下代码获得相同的结果:

library(tidyverse)

# rename column names with vector
colnames(df) <- c("ID", "Math", "Physics", "_", "Computer","PE","English")

df %>%
    pivot_longer(cols= -ID ,names_to="Scale",values_to = "Category") %>% 
    ggplot(aes(fill=Category, x=Scale)) + 
    geom_bar(stat="count", position = 'stack')+
    theme_bw()