R geom_col显示的数值不正确

R geom_col显示的数值不正确,r,R,我试图从李克特的问题中创建一个堆叠的情节。共有4个问题(每个问题的回答人数相同,共204人) 可复制df 问题 价值 选择 问题1 5. 强烈同意 问题1 5. 强烈同意 问题1 4. 同意 问题1 4. 同意 问题2 1. 强烈反对 问题2 1. 强烈反对 问题2 2. 不同意 问题2 3. 中立的 问题3 4. 同意 问题3 3. 中立的 问题3 4. 同意 问题3 5. 强烈同意 问题4 2. 不同意 问题4 1. 强烈反对 问题4 3. 中立的 问题4 4. 同意 一种方法是在绘制数据之

我试图从李克特的问题中创建一个堆叠的情节。共有4个问题(每个问题的回答人数相同,共204人)

可复制df

问题 价值 选择 问题1 5. 强烈同意 问题1 5. 强烈同意 问题1 4. 同意 问题1 4. 同意 问题2 1. 强烈反对 问题2 1. 强烈反对 问题2 2. 不同意 问题2 3. 中立的 问题3 4. 同意 问题3 3. 中立的 问题3 4. 同意 问题3 5. 强烈同意 问题4 2. 不同意 问题4 1. 强烈反对 问题4 3. 中立的 问题4 4. 同意
一种方法是在绘制数据之前进行计数

library(dplyr)
library(ggplot2)

df %>%
  count(question, choices) %>%
  ggplot() +
  geom_col(aes(x = n, y = question, fill = choices), position = "stack") +
  ggtitle("mask likert scale questions") +
  ylab("questions") +
  xlab("# of people") +
  scale_fill_brewer(palette = "RdBu") +
  theme(legend.position = "bottom")

数据

df <- structure(list(question = c("ques1", "ques1", "ques1", "ques1", 
"ques2", "ques2", "ques2", "ques2", "ques3", "ques3", "ques3", 
"ques3", "ques4", "ques4", "ques4", "ques4"), value = c(5L, 5L, 
4L, 4L, 1L, 1L, 2L, 3L, 4L, 3L, 4L, 5L, 2L, 1L, 3L, 4L), choices = c("strongly agree", 
"strongly agree", "agree", "agree", "strongly disagree", "strongly disagree", 
"disagree", "neutral", "agree", "neutral", "agree", "strongly agree", 
"disagree", "strongly disagree", "neutral", "agree")), row.names = c(NA, 
-16L), class = "data.frame")

df我还是得到了和以前一样的图。我还将变量问题和选择改为因素,以防有帮助,但没有效果。相反,我得到了以下错误:错误:美学必须是长度1或与数据(20)相同:XY当您在我的答案中使用代码时,您没有得到如图所示的图形吗?如果您
plyr
已加载,请尝试使用
dplyr::count
。我更新了我的答案,将我使用的数据以可复制的形式包含在内,以便您可以复制并尝试。让我知道你得到了什么。是的,你的代码+数据对我有用,但是,当我在我的完整数据集上尝试它时,它不起作用。我现在在UseMethod(“group_by_”)中得到了这个错误:“group_by_”没有适用于“c('gg','ggplot')”类对象的方法。您用来得到上述错误的确切代码是什么?似乎您正在将代码应用于绘图,而您需要将其应用于数据帧。
covid_survey_masks%%>%dplyr::count(问题,选项)%%>%ggplot()+geom_col(aes(x=value,y=question,fill=choices),position=“stack”)+ggtitle(“mask-likert-scale-questions”)+ylab(“问题”)+xlab(“人”)+scale\u fill\u brewer(palete=“RdBu”)+主题(legend.position=“bottom”)
df <- structure(list(question = c("ques1", "ques1", "ques1", "ques1", 
"ques2", "ques2", "ques2", "ques2", "ques3", "ques3", "ques3", 
"ques3", "ques4", "ques4", "ques4", "ques4"), value = c(5L, 5L, 
4L, 4L, 1L, 1L, 2L, 3L, 4L, 3L, 4L, 5L, 2L, 1L, 3L, 4L), choices = c("strongly agree", 
"strongly agree", "agree", "agree", "strongly disagree", "strongly disagree", 
"disagree", "neutral", "agree", "neutral", "agree", "strongly agree", 
"disagree", "strongly disagree", "neutral", "agree")), row.names = c(NA, 
-16L), class = "data.frame")