簡體   English   中英

在多列中具有不同變量的R條形圖

[英]R bar-plot with different variables in multiple columns

我想在一個圖表中的多個列中創建具有不同變量的R條形圖。 我只能使用以下代碼進行2x2繪圖:

barplot(table(y = cut$Gender,x = cut$Education))

即便如此, 性別仍堆積在教育之上。

受訪者性別和教育程度

我想要的圖表類型如下所示: 在此處輸入圖片說明

我的樣本數據集是:

structure(list(Gender = c("Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Female", "Male", "Male", "Male", "Male", 
"Female", "Male", "Female", "Male", "Male", "Male", "Male"), 
    Age = c("45-54 yrs", "35-44 yrs", "25-34 yrs", "25-34 yrs", 
    "25-34 yrs", "45-54 yrs", "25-34 yrs", "25-34 yrs", "25-34 yrs", 
    "35-44 yrs", "18-24 yrs", "25-34 yrs", "25-34 yrs", "55-64 yrs", 
    "35-44 yrs", "35-44 yrs", "35-44 yrs", "45-54 yrs", "35-44 yrs", 
    "45-54 yrs"), Employment = c("Civil servant", "Private sector", 
    "Private sector", "Private sector", "Trader", "Civil servant", 
    "Private sector", "Private sector", "Private sector", "Civil servant", 
    "Student", "Student", "Civil servant", "Retired", "Self-employed", 
    "Private sector", "Civil servant", "Civil servant", "Private sector", 
    "Private sector"), Marriage = c("Married", "Married", "Married", 
    "Married", "Single, never married", "Married", "Married", 
    "Married", "Married", "Married", "Single, never married", 
    "Single, never married", "Married", "Married", "Married", 
    "Married", "Married", "Married", "Married", "Married"), Education = c("Advanced degree", 
    "Advanced degree", "Bachelor's degree", "Bachelor's degree", 
    "Secondary education", "Advanced degree", "Bachelor's degree", 
    "Bachelor's degree", "Secondary education", "Secondary education", 
    "Secondary education", "Secondary education", "Advanced degree", 
    "Bachelor's degree", "Basic education", "Advanced degree", 
    "Advanced degree", "Advanced degree", "Advanced degree", 
    "Advanced degree"), Residence = c("Ashanti", "Ashanti", "Ashanti", 
    "Ashanti", "Ashanti", "Brong-Ahafo", "Brong-Ahafo", "Brong-Ahafo", 
    "Brong-Ahafo", "Brong-Ahafo", "Brong-Ahafo", "Brong-Ahafo", 
    "Central", "Central", "Eastern", "Greater Accra", "Greater Accra", 
    "Greater Accra", "Greater Accra", "Greater Accra"), Experience = c("Never", 
    "Never", "Never", "Never", "Never", "Never", "Never", "Never", 
    "Never", "Never", "Never", "Never", "Never", "Never", "Never", 
    "Never", "Never", "Never", "Never", "Never")), .Names = c("Gender", 
"Age", "Employment", "Marriage", "Education", "Residence", "Experience"
), row.names = c(NA, 20L), class = "data.frame")

這是一種方法:

首先將數據轉換為長格式,這里有兩個選項可以從reshape包中melt或從tidyr gather 在這里,我將使用tidyverse庫,該庫加載了許多有用的程序包。

library(tidyverse)

 df %>%
      gather(variable, value) 

然后用ggplot2繪制條形圖

ggplot()+
     geom_bar(aes(x = variable, fill = value), color = "black" , position = "stack", show.legend = FALSE)

為了添加文本注釋,我們創建了一個geom_text層,標簽的位置將由stat = "count"確定,它會計算對應於..count..頂部的特殊變量..count.. ,因為這在繪圖上有點粗糙我們可以調整為vjust = 1

geom_text(stat = "count", aes(x = variable, label =  value,
                              y = ..count..,
                              group = value),
          position = "stack", vjust = 1)

要在y軸上添加百分比標簽,通常是y = (..count..)/sum(..count..) ,但是sum(.. count ..)是所有變量的計數總和,而不是在這里合適,所以最簡單的解決方案是手動標記

scale_y_continuous(labels =  c("0%", "25%", "50%", "75%", "100%"),
                   breaks = c(0, 5, 10, 15, 20))

看起來如何:

library(tidyverse)

 df %>%
  gather(variable, value) %>%
  ggplot()+
  geom_bar(aes(x = variable, fill = value),
           color = " black",
           position = "stack", show.legend = FALSE)+
  geom_text(stat = "count",
             aes(x = variable,
                 label =  value,
                 y = ..count..,
                 group = value),
             position = "stack", vjust = 1) +
scale_y_continuous(labels =  c("0%", "25%", "50%", "75%", "100%"),
                   breaks = c(0, 5, 10, 15, 20))

在此處輸入圖片說明

另一個選擇是y = ..count../sum(..count..)*7因為有7個變量

df %>%
  gather(variable, value) %>%
  ggplot()+
  geom_bar(aes(x = variable, y = ..count../sum(..count..)*7, fill = value), color= " black", position = "stack", show.legend = FALSE)+
  geom_text(stat = "count", aes(x = variable, label =  value,  y = ..count../sum(..count..)*7, group = value), position = "stack", vjust = 1)+
  scale_y_continuous(labels = scales::percent)+
  ylab("")

相同的輸出圖

您甚至可以使用帶有gsub和負前瞻的mutate在標簽中添加條件換行符

df %>%
  gather(variable, value) %>% 
  mutate(label = gsub(" (?!yrs)", "\n",  value, perl = T)) %>%
  ggplot()+
  geom_bar(aes(x = variable, y = ..count../sum(..count..)*7, fill = value), color= " black", position = "stack", show.legend = FALSE)+
  geom_text(stat = "count", aes(x = variable, label =  label,  y = ..count../sum(..count..)*7, group = value), position = "stack", vjust = 1)+
  scale_y_continuous(labels = scales::percent)+
  ylab("")

在此處輸入圖片說明

暫無
暫無

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

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