我需要绘制条形图并比较调查的不同项目(数据集中的列)

I need to barplot and compare different items of a survey (columns in the dataset)

我正在为我的硕士论文分析一个数据集。数据来自我创建的一项调查。我正在尽我所能 google 但由于数据集不够稀疏,我遇到了一些长度问题(例如,某些项目的值范围为 1-7,而其他项目的值范围为 2-6。使用的量表是 7 点李克特量表,但如果,例如,一个项目至少有一次没有得到 7,与更稀疏的项目相比,它将具有不同的长度)(问题 1)。

structure(list(AD_BORING_1 = c("3", "2", "4", "1", "6", "3", 
"7", "6", "2", "3", "5", "4", "6", "5", "5", "6", "5", "2", "2", 
"6", "2", "3", "5", "4", "5", "5", "1", "2", "4", "2", "3", "6", 
"5", "5", "3"), AD_IRRITATING_1 = c("3", "2", "2", "1", "7", 
"5", "6", "4", "5", "5", "1", "5", "4", "3", "5", "6", "5", "2", 
"2", "4", "5", "3", "2", "4", "3", "4", "1", "2", "4", "5", "4", 
"4", "7", "4", "2"), AD_DISTURBING_1 = c("3", "1", "3", "3", 
"4", "1", "3", "2", "2", "4", "1", "3", "4", "2", "1", "4", "2", 
"2", "2", "4", "1", "5", "1", "2", "2", "2", "1", "2", "4", "2", 
"4", "2", "4", "6", "2"), AD_CREDIBLE_1 = c("5", "5", "3", "2", 
"1", "2", "6", "3", "6", "3", "5", "4", "2", "3", "4", "1", "5", 
"3", "3", "2", "1", "3", "5", "3", "2", "4", "6", "6", "3", "1", 
"5", "6", "2", "3", "5"), AD_GOOD_1 = c("5", "5", "3", "2", "2", 
"5", "3", "4", "5", "2", "5", "2", "1", "5", "4", "2", "2", "5", 
"5", "2", "3", "5", "4", "4", "4", "4", "6", "4", "3", "2", "4", 
"4", "1", "4", "5"), AD_HONEST_1 = c("5", "3", "3", "2", "2", 
"1", "4", "3", "5", "2", "6", "1", "2", "2", "3", "2", "4", "3", 
"2", "2", "2", "3", "2", "4", "1", "3", "4", "3", "2", "2", "3", 
"5", "1", "4", "3"), AD_TRUTHFUL_1 = c("5", "3", "4", "2", "2", 
"1", "5", "3", "5", "2", "5", "2", "2", "3", "3", "2", "5", "3", 
"2", "1", "2", "2", "4", "5", "1", "3", "4", "4", "4", "1", "2", 
"3", "1", "1", "3"), AD_LIKEABLE_1 = c("5", "4", "3", "2", "2", 
"6", "2", "4", "5", "4", "4", "3", "3", "4", "3", "4", "5", "6", 
"7", "1", "2", "2", "2", "4", "1", "3", "6", "6", "2", "4", "1", 
"4", "1", "3", "5"), AD_ENJOYABLE_1 = c("5", "5", "3", "2", "2", 
"4", "2", "4", "5", "4", "5", "3", "2", "6", "3", "2", "5", "6", 
"7", "2", "2", "2", "4", "5", "2", "3", "7", "6", "3", "4", "4", 
"3", "1", "3", "4"), LIKE_1 = c("6", "5", "3", "2", "1", "4", 
"2", "3", "5", "3", "4", "3", "1", "4", "3", "3", "5", "5", "7", 
"1", "4", "5", "4", "4", "2", "4", "6", "6", "4", "3", "4", "4", 
"1", "2", "5")), row.names = c(NA, -35L), class = c("tbl_df", 
"tbl", "data.frame"))

主数据集的行正好是第n行。的观察结果和每个项目得分都在列中。

另一个问题是我不知道如何将它们正确地绘制在一起以便在一个简单的条形图中进行比较,例如下图:

我尝试使用此代码处理相同长度的项目:

prova <- data.frame(table(A_DF_GIL$AD_BORING_1), table(A_DF_GIL$AD_IRRITATING_1))
barplot(as.matrix(prova))

结果还是不是我想要的。有人可以帮我吗?谢谢你

现在我对你的目标有了更好的了解,所以这是一个更新的回复,你的数据集更完整。

在处理数据时,我使用了带有 regex 的重命名函数来清理名称,但这是可选的。我还将分数转换为 factor,因此很容易将它们视为有序离散数据(它们是)而不是连续数据。但是,我在底部示例中转换回连续数据以计算 mean(),这是减少情节的一种选择。

鉴于体积较大,我选择了使用 geom_bar(position = "stack") 的堆积条形图,但请尝试 "dodge" 亲自看看。

我还通过 LIKE 注释掉了 facet 行,但您应该尝试一下,看看它是否提供更多信息。

我还应用了一些我主观上喜欢的美学,但主要是为了证明 {ggplot2} 中有很多您可以自定义的控件。

我将李克特刻度标签应用到色标上,如果有帮助,您可以对其进行自定义,或者通过删除 labels = likert_scale.

来省略
library(tidyverse)

d <- structure(list(AD_BORING_1 = c("3", "2", "4", "1", "6", "3", "7", "6", "2", "3", "5", "4", "6", "5", "5", "6", "5", "2", "2", "6", "2", "3", "5", "4", "5", "5", "1", "2", "4", "2", "3", "6", "5", "5", "3"), AD_IRRITATING_1 = c("3", "2", "2", "1", "7", "5", "6", "4", "5", "5", "1", "5", "4", "3", "5", "6", "5", "2", "2", "4", "5", "3", "2", "4", "3", "4", "1", "2", "4", "5", "4", "4", "7", "4", "2"), AD_DISTURBING_1 = c("3", "1", "3", "3", "4", "1", "3", "2", "2", "4", "1", "3", "4", "2", "1", "4", "2", "2", "2", "4", "1", "5", "1", "2", "2", "2", "1", "2", "4", "2", "4", "2", "4", "6", "2"), AD_CREDIBLE_1 = c("5", "5", "3", "2", "1", "2", "6", "3", "6", "3", "5", "4", "2", "3", "4", "1", "5", "3", "3", "2", "1", "3", "5", "3", "2", "4", "6", "6", "3", "1", "5", "6", "2", "3", "5"), AD_GOOD_1 = c("5", "5", "3", "2", "2", "5", "3", "4", "5", "2", "5", "2", "1", "5", "4", "2", "2", "5", "5", "2", "3", "5", "4", "4", "4", "4", "6", "4", "3", "2", "4", "4", "1", "4", "5"), AD_HONEST_1 = c("5", "3", "3", "2", "2", "1", "4", "3", "5", "2", "6", "1", "2", "2", "3", "2", "4", "3", "2", "2", "2", "3", "2", "4", "1", "3", "4", "3", "2", "2", "3", "5", "1", "4", "3"), AD_TRUTHFUL_1 = c("5", "3", "4", "2", "2", "1", "5", "3", "5", "2", "5", "2", "2", "3", "3", "2", "5", "3", "2", "1", "2", "2", "4", "5", "1", "3", "4", "4", "4", "1", "2", "3", "1", "1", "3"), AD_LIKEABLE_1 = c("5", "4", "3", "2", "2", "6", "2", "4", "5", "4", "4", "3", "3", "4", "3", "4", "5", "6", "7", "1", "2", "2", "2", "4", "1", "3", "6", "6", "2", "4", "1", "4", "1", "3", "5"), AD_ENJOYABLE_1 = c("5", "5", "3", "2", "2", "4", "2", "4", "5", "4", "5", "3", "2", "6", "3", "2", "5", "6", "7", "2", "2", "2", "4", "5", "2", "3", "7", "6", "3", "4", "4", "3", "1", "3", "4"), LIKE_1 = c("6", "5", "3", "2", "1", "4", "2", "3", "5", "3", "4", "3", "1", "4", "3", "3", "5", "5", "7", "1", "4", "5", "4", "4", "2", "4", "6", "6", "4", "3", "4", "4", "1", "2", "5")), row.names = c(NA, -35L), class = c("tbl_df", "tbl", "data.frame"))

# make labels in case that helps
likert_breaks <- c("Strongly Disagree", "Somewhat Disagree", "Slightly Disagree", "Neutral", "Slightly Agree", "Somewhat Agree", "Strongly Agree")

# process and plot as stacked bar plot with optional faceting
d %>%
  rename_with(~str_extract(.x, "(?<=_)(.*)(?=_)"), contains("AD")) %>%
  rename(LIKE = LIKE_1) %>%
  mutate(across(everything(), as.integer)) %>%
  mutate(respondent = row_number()) %>%
  pivot_longer(-c(respondent, LIKE), names_to = "adjective", values_to = "likert") %>%
  mutate(likert = factor(likert)) %>%
  ggplot(aes(adjective, fill = likert)) +
  geom_bar(stat = "count", position = "stack") +
  # facet_wrap(~LIKE) +
  scale_fill_viridis_d(option = "A", begin = 0, end = 0.85, labels = likert_breaks) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90))

另一个让事情更紧凑的选择是只显示每个形容词的平均分数。在这种情况下,您需要将其保留为 numeric 以便您可以应用 mean().

之类的汇总函数
# just show mean likert scores for each
d %>%
  rename_with(~str_extract(.x, "(?<=_)(.*)(?=_)"), contains("AD")) %>%
  rename(LIKE = LIKE_1) %>%
  mutate(across(everything(), as.integer)) %>%
  mutate(respondent = row_number()) %>%
  pivot_longer(-c(respondent, LIKE), names_to = "adjective", values_to = "likert") %>%
  group_by(adjective) %>%
  summarise(likert = mean(likert)) %>%
  ggplot(aes(reorder(adjective, -likert), likert)) +
  geom_col() +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90))

reprex package (v2.0.1)

创建于 2022-02-08