简体   繁体   English

如何通过在 x 轴上使用两个变量和在 y 轴上使用分组变量来制作条形图?

[英]How to make a bar-chart by using two variables on x-axis and a grouped variable on y-axis?

I hope I asked my question in the right way this time: If not let me know!我希望这次我以正确的方式问我的问题:如果没有让我知道! I want to code a grouped bar-chart similary to this one (I just created in paint): enter image description here I created as flipped both it actually doesn't matter if its flipped or not.我想编写一个与此类似的分组条形图(我刚刚在绘画中创建):在此处输入图像描述我创建为翻转两者实际上它是否翻转并不重要。 So, a plot similarly to this will also be very usefull: Grouped barchart in r with 4 variables因此,与此类似的 plot 也将非常有用: Grouped barchart in r with 4 variables

Both the variables, happy and lifesatisfied are scaled values from 0 to 10. Working hours is a grouped value and contains 43+, 37-42, 33-36, 27-32, and <27.变量 happy 和 lifesatisfied 都是从 0 到 10 的标度值。工作时间是一个分组值,包含 43+、37-42、33-36、27-32 和 <27。

A very similar example of how my data set looks like (I just changed the values and order, I also have much more observations):我的数据集的一个非常相似的例子(我只是改变了值和顺序,我还有更多的观察):

Working hours工作时间 happy快乐的 lifestatisfied生活状态 contry国家
37-42 37-42 7 7 9 9 DK DK
<27 <27 8 8个 8 8个 SE SE
43+ 43+ 7 7 8 8个 DK DK
33-36 33-36 6 6个 6 6个 SE SE
37-42 37-42 7 7 5 5个 NO
<27 <27 4 4个 7 7 NO

I tried to found similar examples and based on that tried to code the bar chart in the following way but it doesn't work:我试图找到类似的示例,并基于此尝试以下列方式对条形图进行编码,但它不起作用:

df2 <- datafilteredwomen %>% 
  pivot_longer(cols = c("happy", "stflife"), names_to = "var", values_to = "Percentage")

ggplot(df2) +
  geom_bar(aes(x = Percentage, y = workinghours, fill = var ), stat = "identity", position = "dodge") + theme_minimal()

It give this plot which is not correct/what I want: enter image description here它给这个 plot 这是不正确的/我想要的:在此处输入图像描述

seocnd try:第二次尝试:

forplot = datafilteredwomen %>% group_by(workinghours, happy, stflife) %>% summarise(count = n()) %>% mutate(proportion = count/sum(count))

ggplot(forplot, aes(workinghours, proportion, fill = as.factor(happy))) + 
  geom_bar(position = "dodge", stat = "identity", color = "black") 

gives this plot: enter image description here给出这个 plot:在此处输入图片描述

third try - used the ggplot2 builder add-in:第三次尝试 - 使用 ggplot2 构建器插件:

library(dplyr)
library(ggplot2)

datafilteredwomen %>%
 filter(!is.na(workinghours)) %>%
 ggplot() +
 aes(x = workinghours, group = happy, weight = happy) +
 geom_bar(position = "dodge", 
 fill = "#112446") +
 theme_classic() + scale_y_continuous(labels = scales::percent)

gives this plot: enter image description here给出这个 plot:在此处输入图片描述

But none of my tries are what I want.. really hope that someone can help me if it's possible!但是我的尝试都不是我想要的..真的希望有人能帮助我,如果可能的话!

Taking this example dataframe df :以这个例子 dataframe df

df <- structure(list(Working.hours = c("37-42", "37-42", "<27", "<27", 
"43+", "43+", "33-36", "33-36", "37-42", "37-42", "<27", "<27"
), country = c("DK", "DK", "SE", "SE", "DK", "DK", "SE", "SE", 
"NO", "NO", "NO", "NO"), criterion = c("happy", "lifesatisfied", 
"happy", "lifesatisfied", "happy", "lifesatisfied", "happy", 
"lifesatisfied", "happy", "lifesatisfied", "happy", "lifesatisfied"
), score = c(7L, 9L, 8L, 8L, 7L, 8L, 6L, 6L, 7L, 5L, 4L, 7L)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

you can proceed like this:你可以这样进行:

library(dplyr)
library(ggplot2)

df <- 
    df %>%
    pivot_longer(cols = c(happy, lifesatisfied),
                 names_to = 'criterion',
                 values_to = 'score'
                 )

df %>%
    ggplot(aes(x = Working.hours,
            y = score,
            fill = criterion)) +
        geom_col(position = 'dodge') +
        coord_flip()

For picking colours see ?scale_fill_manual , for formatting legend etc. numerous existing answers to related questions on stackoverflow.对于选择颜色,请参见?scale_fill_manual ,对于格式化图例等,stackoverflow 上相关问题的许多现有答案。

After speaking to the OP I found his data source and came up with this solution.在与 OP 交谈后,我找到了他的数据源并提出了这个解决方案。 Apologies if it's a bit messy, I have only been using R for 6 months.抱歉,如果有点乱,我只使用 R 6 个月。 For ease of reproducibility I have preselected the variables used from the original dataset.为了便于再现,我预先选择了从原始数据集中使用的变量。

data <- structure(list(wkhtot = c(40, 8, 50, 40, 40, 50, 39, 48, 45, 
16, 45, 45, 52, 45, 50, 37, 50, 7, 37, 36), happy = c(7, 8, 10, 
10, 7, 7, 7, 6, 8, 10, 8, 10, 9, 6, 9, 9, 8, 8, 9, 7), stflife = c(8, 
8, 10, 10, 7, 7, 8, 6, 8, 10, 9, 10, 9, 5, 9, 9, 8, 8, 7, 7)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

Here are the packages required.这是所需的包。

require(dplyr)
require(ggplot2)
require(tidyverse)
    

Here I have manipulated the data and commented my reasoning.在这里,我操纵了数据并评论了我的推理。

data <- data %>%
  select(wkhtot, happy, stflife) %>% #Select the wanted variables
  rename(Happy = happy) %>% #Rename for graphical sake
  rename("Life Satisfied" = stflife) %>%
  na.omit() %>% # remove NA values
  group_by(WorkingHours = cut(wkhtot, c(-Inf, 27, 32,36,42,Inf))) %>% #Create the ranges
  select(WorkingHours, Happy, "Life Satisfied") %>% #Select the variables again
  pivot_longer(cols = c(`Happy`, `Life Satisfied`), names_to = "Criterion", values_to = "score") %>% # pivot the df longer for plotting
  group_by(WorkingHours, Criterion)

data$Criterion <- as.factor(data$Criterion) #Make criterion a factor for graphical reasons

A bit more data prep更多的数据准备

# Creating the percentage
data.plot <- data %>%
  group_by(WorkingHours, Criterion) %>%
  summarise_all(sum) %>% # get the sums for score by working hours and criterion
  group_by(WorkingHours) %>%
  mutate(tot = sum(score)) %>%
  mutate(freq =round(score/tot *100, digits = 2)) # get percentage
  

Creating the plot.创建 plot。

# Plotting
ggplot(data.plot, aes(x = WorkingHours, y = freq,  fill = Criterion)) +
  geom_col(position = "dodge") +
  geom_text(aes(label = freq), 
            position = position_dodge(width = 0.9), 
            vjust = 1) +
  xlab("Working Hours") +
  ylab("Percentage")  

Please let me know if there is a more concise or easier way!!如果有更简洁或更简单的方法,请告诉我!!

B

DataSource: https://www.europeansocialsurvey.org/downloadwizard/?fbclid=IwAR2aVr3kuqOoy4mqa978yEM1sPEzOaghzCrLCHcsc5gmYkdAyYvGPJMdRp4数据源: https://www.europeansocialsurvey.org/downloadwizard/?fbclid=IwAR2aVr3kuqOoy4mqa978yEM1sPEzOaghzCrLCHcsc5gmYkdAyYvGPJMdRp4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM