簡體   English   中英

ggplot中的堆積百分比條形圖出錯

[英]Stacked percentage bar chart went wrong in ggplot

我想創建一個堆積百分比條形圖,但出了點問題。 我可以創建一個普通的堆疊條形圖,但是一旦我在 position 下將“堆疊”替換為“填充”,它就會給我這個不正確的條形圖:

在此處輸入圖像描述

我使用了這段代碼:

ggplot(dataset, aes(fill = family_role_recoded, x=country, y=family_role_recoded)) + geom_bar(position="fill", stat="identity")

這是一個可重現的示例:

structure(list(country = c("Lebanon", "Algeria", "Iraq", "Jordan", 
"Morocco", "Lebanon", "Lebanon", "Lebanon", "Lebanon", "Lebanon", 
"Lebanon", "Lebanon", "Lebanon", "Lebanon", "Lebanon", "Lebanon", 
"Lebanon", "Lebanon", "Lebanon", "Lebanon", "Lebanon", "Lebanon", 
"Lebanon", "Lebanon", "Lebanon"), urban_rural = structure(c(2L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), levels = c("Rural", "Urban", 
"Refugee camp"), class = "factor"), age = c(41L, 36L, 33L, 26L, 
28L, 33L, 31L, 45L, 70L, 18L, 23L, 20L, 24L, 44L, 38L, 39L, 23L, 
45L, 26L, 54L, 26L, 22L, 33L, 62L, 18L), education = c("Preparatory/Basic", 
"Preparatory/Basic", "Secondary", "Elementary", "Mid-level diploma/professional or technical", 
"Secondary", "Elementary", "Elementary", "Elementary", "Secondary", 
"BA", "Secondary", "BA", "Preparatory/Basic", "Elementary", "BA", 
"Secondary", "BA", "Preparatory/Basic", "Secondary", "Secondary", 
"Preparatory/Basic", "Preparatory/Basic", "Preparatory/Basic", 
"Secondary"), family_role = structure(c(1L, 2L, 2L, 1L, 2L, 2L, 
2L, 3L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, 3L, 3L, 2L, 2L, 4L, 2L, 2L, 
3L, 1L, 3L), levels = c("I strongly agree", "I agree", "I disagree", 
"I strongly disagree", "Don't know", "Refused to answer"), class = "factor"), 
    religious = structure(c(2L, 1L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 
    3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L), levels = c("Religious", "Somewhat religious", "Not religious"
    ), class = "factor"), gender = c("Male", "Female", "Female", 
    "Male", "Male", "Male", "Male", "Male", "Male", "Male", "Female", 
    "Male", "Male", "Female", "Male", "Female", "Female", "Male", 
    "Female", "Female", "Male", "Male", "Male", "Female", "Male"
    ), education_cat = structure(c(2L, 2L, 3L, 2L, 3L, 3L, 2L, 
    2L, 2L, 3L, 4L, 3L, 4L, 2L, 2L, 4L, 3L, 4L, 2L, 3L, 3L, 2L, 
    2L, 2L, 3L), levels = c("No formal education", "Basic education", 
    "Secondary education", "Higher education"), class = "factor"), 
    family_role_recoded = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 
    2L, 1L, 2L, 1L), levels = c("Disagree/strongly disagree", 
    "Agree/strongly agree", "Don't know"), class = "factor")), row.names = c(NA, 
25L), class = "data.frame")

我很感激任何建議!

此致

我的建議如下:

library(ggplot2)

library(scales)

df|> ggplot(aes(x=country, fill = family_role_recoded))+ geom_bar(position = "fill")+ scale_y_continuous(labels=scales::percent)

我會建議這樣的事情:


    df_agg <- df %>% 
      group_by(country, family_role_recoded) %>% 
      summarise(count = n()) %>% 
      ungroup() %>% 
      group_by(country) %>% 
      mutate(total = sum(count)) %>% 
      ungroup() %>% 
      mutate(percentage = count/total)

    levels <- df_agg$country[df_agg$family_role_recoded=="Agree/strongly agree"][rev(order(df_agg$percentage[df_agg$family_role_recoded=="Agree/strongly agree"]))]
    df_agg$country <- factor(df_agg$country, levels = levels)
    
    g1 <- ggplot(df_agg, aes(fill=family_role_recoded, x=country, y = percentage )) + 
      geom_bar(position="fill", stat="identity")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM