简体   繁体   English

在多列中具有不同变量的R条形图

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

I want to create an R bar-plot with different variables in multiple columns, all in one chart. 我想在一个图表中的多个列中创建具有不同变量的R条形图。 I am only able to do a 2x2 plot with the following code: 我只能使用以下代码进行2x2绘图:

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

Even so, Gender gets stacked on top of Education . 即便如此, 性别仍堆积在教育之上。

受访者性别和教育程度

The type of chart I want looks like this: 我想要的图表类型如下所示: 在此处输入图片说明

My sample dataset is: 我的样本数据集是:

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")

Here is an approach: 这是一种方法:

First convert the data to long format, here one has two options melt from reshape package or gather from tidyr . 首先将数据转换为长格式,这里有两个选项可以从reshape包中melt或从tidyr gather Here I will use tidyverse library which loads many useful packages. 在这里,我将使用tidyverse库,该库加载了许多有用的程序包。

library(tidyverse)

 df %>%
      gather(variable, value) 

Then make a bar plot with ggplot2 然后用ggplot2绘制条形图

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

To add text annotations we make a geom_text layer, the positions of the labels will be determined by stat = "count" which calculates a special variable ..count.. corresponding to the top of the bars since this is a bit crude on the plot we can adjust it with vjust = 1 为了添加文本注释,我们创建了一个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)

To add percent labels on y axis the usual is y = (..count..)/sum(..count..) , however the sum(..count..) is the sum of counts across all variables and is not appropriate here so the easiest solution is to manually label 要在y轴上添加百分比标签,通常是y = (..count..)/sum(..count..) ,但是sum(.. count ..)是所有变量的计数总和,而不是在这里合适,所以最简单的解决方案是手动标记

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

How it looks all together: 看起来如何:

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))

在此处输入图片说明

another option is y = ..count../sum(..count..)*7 since there are 7 variables 另一个选择是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("")

same output graph 相同的输出图

You can even add a conditional line break in the labels using mutate with gsub and negative lookahead 您甚至可以使用带有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