在ggplot2中编码多个响应变量

问题描述 投票:0回答:1

对于有关人们职业的问题,我有多个回复数据。它们都在名为“职业”的列中,以逗号分隔(农民、公务员),但我无法使用这些数据创建条形图。 我无法创建新列,将这些数据分开以绘制每个数据的比例(与参与者数量相关)。我在搜索视频时发现了这个脚本,但它给了我一个错误。

library(dplyr)
library(tidyr) 
library(readr)
library(ggplot2)

dados <- readr::read_csv2("ocupacao_mulheres_2.csv")
glimpse(dados)

dados2 <- dados %>% 
  mutate(value = "Yes") %>% 
  tidyr::separate_rows(occupation , sep = ", ") %>% 
  tidyr::pivot_wider(names_from = occupation,
                     values_fill = "No") %>%
  dplyr::summarise(n = dplyr::n(), .by = c(community, area, area2,
                                           occupation)) |>
  dplyr::filter(n > 1L)

dados2 %>% 
  tidyr::pivot_longer(cols = 2:10, names_to = "Meio") %>% 
  ggplot(aes(x = Meio, fill = value)) +
  geom_bar() +
  labs(y = "Frequência (n)", x = NULL, fill = NULL) +
  coord_flip() +
  theme_bw() +
  theme(legend.position = "bottom")

> dput(ocupacaom)
structure(list(community = c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 3L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 
7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 
18L, 18L, 18L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 23L, 23L, 
23L, 23L, 23L, 23L, 24L, 25L, 27L, 28L, 28L, 29L, 29L, 29L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 
31L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 
11L, 11L, 11L, 11L, 11L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 23L, 23L, 23L, 24L, 24L, 
24L, 25L, 26L, 27L, 28L, 28L, 29L, 30L, 31L, 31L, 31L, 31L, 31L, 
31L, 32L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 2L, 2L, 
1L, 13L, 20L, 1L, 1L, 6L, 6L, 7L, 8L, 9L, 9L, 10L, 10L, 11L, 
12L, 13L, 13L, 14L, 14L, 16L, 16L, 16L, 17L, 17L, 18L, 18L, 18L, 
19L, 19L, 19L, 21L, 21L, 21L, 22L, 22L, 23L, 23L, 27L, 30L, 31L, 
31L, 32L, 32L, 33L, 33L, 17L, 30L, 11L, 9L, 25L, 2L, 2L, 15L, 
32L), occupation = c("Cleaning assistant", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", 
"Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife", 
"Farmer, Housewife", "Farmer, Housewife", "Farmer, Housewife, Fisherwoman, Public servant", 
"Farmer, Housewife, Public servant", "Farmer, Lunch lady", "Farmer, Lunch lady", 
"Farmer, Lunch lady", "Farmer, Public servant", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife", "Housewife", "Housewife", "Housewife", "Housewife", 
"Housewife, Canteen worker", "Housewife, Fisherwoman", "Housewife, Public servant", 
"Housewife, Retired", "Housewife, Teacher", "Public servant", 
"Public servant", "Public servant", "Retired"))), class = "data.frame", row.names = c(NA, 
-334L))
>

有人可以帮助我吗?

我尝试运行“values_fill =“No”,期望他将每个响应分隔在不同的列中。在空白处,他会输入“否”,在其他字段中输入职业。

r ggplot2 bar-chart tidyr
1个回答
0
投票

我不确定我完全理解目标是什么,所以在这里你可以找到我的解释(我不完全精通 dplyr)。

我假设

dados
(我在下面的代码中使用)是您发布的表格。

# load libraries
library(data.table)
library(dplyr)

# split column occupation into multiple columns
wide <- cbind.data.frame(dados$community, str_split_fixed(gsub(" ", "", dados$occupation), ",", 4)) # using 4 because that's the maximum number of occupations a person has
# assign colnames
colnames(wide) <- c("community", "occupation_one", "occupation_two", "occupation_three", "occupation_four")

# reshape occupations into a single column
long <- melt(setDT(wide), id.vars = c("community"), variable.name = "occupation")
# add unemployed instead of ""
long$value[long$value==""] <- "Unemployed"


# group to plot
long %>%
    group_by(community, value) %>%
    summarise(n = n()) %>%
    mutate(freq = n / sum(n)) %>% 
    ggplot() +
    geom_bar(aes(x=value, y=n), stat="identity") +
    facet_wrap(~community) +
    ylab("frequency") +
    xlab("") +
    theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1))

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.