五个等级和四个类别的堆叠条

我需要在已放样的钢筋上展示我的数据。五个量表共四类,样本量为200

df <- tribble(
    ~ID,~scale1,~scale2,~scale3,~scale4,~scale5,"1","Cat1","Cat2","Cat4","2","Cat3","3","4","5","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","Cat1")))%>%
  mutate(Scale=factor(Scale,levels =c("Math","Physics","","Computer","PE","English")))
hcmove 回答:五个等级和四个类别的堆叠条

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,x=Scale)) + 
    geom_bar(position="fill",stat="identity") +
    geom_text(aes(label=paste0(sprintf("%1.1f",percent*100),"%")),position=position_fill(vjust=0.5),size = 5) +
    scale_y_continuous(labels = scales::percent)

enter image description here

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

library(tidyverse)

# rename column names with vector
colnames(df) <- c("ID","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()
    

enter image description here

,

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

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

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


df=df %>%
  select(ID,scale1:scale5) %>%
  pivot_longer(cols=scale1:scale5,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","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()

enter image description here

本文链接:https://www.f2er.com/7904.html

大家都在问