Stacked Barplot พร้อมความถี่นับ ggplot2

Oct 01 2020

ฉันมีชุดข้อมูลที่ฉันมีเงื่อนไขหลายอย่างและฉันต้องการสร้างกราฟแท่งแบบเรียงซ้อนซึ่งแสดงความถี่ของข้อผิดพลาดที่เกิดขึ้นในแต่ละเงื่อนไข (ดังนั้นจำนวนกรณีในแต่ละเงื่อนไขที่เกิดข้อผิดพลาด 1 ครั้งเกิดข้อผิดพลาด 2 ข้อ 3 ข้อผิดพลาด ... ฯลฯ ฯลฯ )

ตามทฤษฎีแล้วฉันเข้าใจหลักการสร้างกราฟแท่งด้วย ggplot2 อย่างไรก็ตามปัญหาที่ฉันพบคือจำนวน 'ความถี่' ไม่ใช่ตัวแปรจริงในกรอบข้อมูล (เนื่องจากต้องนับจำนวนกรณี) ฉันไม่แน่ใจว่าจะเพิ่มลงใน gpplot2 framework ได้อย่างไร (อาจใช้ฟังก์ชัน 'stat' แต่ฉันไม่แน่ใจว่ามันทำงานอย่างไร)

ฉันตรวจสอบคำถามที่คล้ายกันดังต่อไปนี้:

วิธี barplot ความถี่ด้วย ggplot2?

R เรียงซ้อน% ฮิสโตแกรมความถี่ที่มีเปอร์เซ็นต์ของข้อมูลรวมตาม

แสดงความถี่แทนการนับด้วย geom_bar () ใน ggplot

วิธีติดป้ายฮิสโตแกรมแบบเรียงซ้อนใน ggplot

แต่ไม่มีคำตอบใดที่ให้คำตอบที่ฉันต้องการได้ (เช่นวิธีการนับจำนวนกรณีสำหรับแต่ละ 'ข้อผิดพลาด' และรวมสิ่งนั้นไว้ในรหัส ggplot2

ด้านล่างนี้เป็นความพยายามบางส่วนของฉันกับข้อมูลตัวอย่าง

library(tidyverse)

condition <- c("condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3")
number_of_errors <- c(1,2,3,3,2,1,4,4,5,4,5,1,2,2,3)

df <- data.frame(condition, number_of_errors)
df

df_melt <-melt(df) #This creates a data frame with 3 columns, 'condition', 'variable' and 'value' where 'variable' just says 'number_of_errors' for each row


# Attempt 1 - (Error: stat_bin() can only have an x or y aesthetic.)
ggplot(df_melt, aes(x=condition, y = variable, fill=value)) + 
  geom_bar(stat="bin", position="stack") +
  xlab("Condition") + 
  ylab("Frequency of Errors")


# Attempt 2 (produces a graph, but not a stacked one, just the total number of cases in each condition)
ggplot(df_melt, aes(x = condition, fill = value, label = value)) +
  geom_bar(col="black") +
  stat_count(position="stack")


# Attempt 3 (also produces a graph, but again not a stacked one - I think it is the sum of the number of errors?)
ggplot(df_melt,aes(factor(condition),y=as.numeric(value))) + 
  geom_bar(stat = "identity", position = "stack")

ฉันแน่ใจว่าฉันต้องพลาดอะไรบางอย่างที่ชัดเจนเกี่ยวกับวิธีสร้างค่าสำหรับการนับ แต่ฉันไม่แน่ใจว่าอะไร ขอขอบคุณคำแนะนำใด ๆ :)

คำตอบ

1 ChuckP Oct 01 2020 at 15:29

ฉันคิดว่ากุญแจสำคัญสำหรับคุณคือการแปลงnumber_of_errorsเป็นปัจจัยและทำให้geom_bar(stat="count")คุณได้รับจากบทช่วยสอนนี้

library(ggplot2)
df$number_of_errors <- factor(df$number_of_errors)

ggplot(df, aes(x=condition, fill = number_of_errors)) +
  geom_bar(stat="count")

2 Duck Oct 01 2020 at 15:25

บางทีคุณอาจกำลังมองหาพล็อตสไตล์นี้ คุณต้องจัดกลุ่มตามเงื่อนไขแล้วกำหนดค่าเพื่อให้สามารถออกแบบแท่งได้ นี่คือรหัส:

library(tidyverse)
#Data
condition <- c("condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3", "condition 1", "condition 2", "condition 3")
number_of_errors <- c(1,2,3,3,2,1,4,4,5,4,5,1,2,2,3)
df <- data.frame(condition, number_of_errors)
#Code
df %>% group_by(condition) %>% mutate(Number=factor(1:n())) %>%
  ggplot(aes(x=condition,y=number_of_errors,fill=Number,group=Number))+
  geom_bar(stat = 'identity')+
  geom_text(aes(label=number_of_errors),position = position_stack(0.5))+
  theme(legend.position = 'none')

เอาท์พุต: