R多项选择问卷数据到ggplot

R多项选择问卷数据到ggplot,r,ggplot2,survey,qualtrics,R,Ggplot2,Survey,Qualtrics,我有一个Qualtrics多项选择题,我想用它在R中创建图表。我的数据是有组织的,这样你就可以为每个问题回答多个答案。例如,参与者1选择多项选择题答案1(Q1_1)和3(Q1_3)。我想把所有答案折叠成一个条形图,每个多重回答选项(Q1_1:Q1_3)用一个条形图除以回答此问题的受访者人数(在本例中为3) df这里是一个使用dplyr包中的ddply的解决方案 # I needed to increase number of participants to ensure it works in

我有一个Qualtrics多项选择题,我想用它在R中创建图表。我的数据是有组织的,这样你就可以为每个问题回答多个答案。例如,参与者1选择多项选择题答案1(Q1_1)和3(Q1_3)。我想把所有答案折叠成一个条形图,每个多重回答选项(Q1_1:Q1_3)用一个条形图除以回答此问题的受访者人数(在本例中为3)


df这里是一个使用
dplyr
包中的
ddply
的解决方案

# I needed to increase number of participants to ensure it works in every case
df = data.frame(Participant = seq(1:100), 
Q1_1 = sample(c("a", ""), 100, replace = T, prob = c(1/2, 1/2)), 
Q1_2 = sample(c("b", ""), 100, replace = T, prob = c(2/3, 1/3)), 
Q1_3 = sample(c("c", ""), 100, replace = T, prob = c(1/3, 2/3)))
df$answer = paste0(df$Q1_1, df$Q1_2, df$Q1_3)

summ = ddply(df, c("answer"), summarize, freq = length(answer)/nrow(df))

## Re-ordeing of factor levels summ$answer
summ$answer <- factor(summ$answer, levels=c("", "a", "b", "c", "ab", "ac", "bc", "abc"))

# Plot 
ggplot(summ, aes(answer, freq, fill = answer)) + geom_bar(stat = "identity") + theme_bw() 
#我需要增加参与者的数量,以确保它在任何情况下都有效
df=数据帧(参与者=序号(1:100),
Q1_1=样本(c(“a”),100,替换=T,概率=c(1/2,1/2)),
Q1_2=样本(c(“b”),100,替换=T,概率=c(2/3,1/3)),
Q1_3=样本(c(“c”),100,替换=T,概率=c(1/3,2/3)))
df$answer=paste0(df$Q1_1、df$Q1_2、df$Q1_3)
summ=ddply(df,c(“答案”),summary,freq=length(答案)/nrow(答案))
##因子水平的重新排序汇总$答案

summ$answer这是一个使用
dplyr
包中的
ddply
的解决方案

# I needed to increase number of participants to ensure it works in every case
df = data.frame(Participant = seq(1:100), 
Q1_1 = sample(c("a", ""), 100, replace = T, prob = c(1/2, 1/2)), 
Q1_2 = sample(c("b", ""), 100, replace = T, prob = c(2/3, 1/3)), 
Q1_3 = sample(c("c", ""), 100, replace = T, prob = c(1/3, 2/3)))
df$answer = paste0(df$Q1_1, df$Q1_2, df$Q1_3)

summ = ddply(df, c("answer"), summarize, freq = length(answer)/nrow(df))

## Re-ordeing of factor levels summ$answer
summ$answer <- factor(summ$answer, levels=c("", "a", "b", "c", "ab", "ac", "bc", "abc"))

# Plot 
ggplot(summ, aes(answer, freq, fill = answer)) + geom_bar(stat = "identity") + theme_bw() 
#我需要增加参与者的数量,以确保它在任何情况下都有效
df=数据帧(参与者=序号(1:100),
Q1_1=样本(c(“a”),100,替换=T,概率=c(1/2,1/2)),
Q1_2=样本(c(“b”),100,替换=T,概率=c(2/3,1/3)),
Q1_3=样本(c(“c”),100,替换=T,概率=c(1/3,2/3)))
df$answer=paste0(df$Q1_1、df$Q1_2、df$Q1_3)
summ=ddply(df,c(“答案”),summary,freq=length(答案)/nrow(答案))
##因子水平的重新排序汇总$答案

summ$answer也许这就是你想要的

f <- 
  structure(
    list(
      Participant = 1:3,
      A = c("a", "a", ""),
      B = c("", "b", "b"),
      C = c("c", "c", "c")),
    .Names = c("Participant", "Q1_1", "Q1_2", "Q1_3"),
    row.names = c(NA, -3L),
    class = "data.frame"
  )


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

nparticipant <- nrow(f)
f %>% 
  ## Reformat the data
  gather(question, response, starts_with("Q")) %>%
  filter(response != "") %>%

  ## calculate the height of the bars
  group_by(question) %>%
  summarise(score = length(response)/nparticipant) %>%

  ## Plot
  ggplot(aes(x=question, y=score)) +
  geom_bar(stat = "identity")
f%
过滤器(响应!=“”)%>%
##计算钢筋的高度
组别(问题)%>%
总结(分数=长度(反应)/参与者)%>%
##密谋
ggplot(aes(x=问题,y=分数))+
几何图形栏(stat=“identity”)

也许这就是你想要的

f <- 
  structure(
    list(
      Participant = 1:3,
      A = c("a", "a", ""),
      B = c("", "b", "b"),
      C = c("c", "c", "c")),
    .Names = c("Participant", "Q1_1", "Q1_2", "Q1_3"),
    row.names = c(NA, -3L),
    class = "data.frame"
  )


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

nparticipant <- nrow(f)
f %>% 
  ## Reformat the data
  gather(question, response, starts_with("Q")) %>%
  filter(response != "") %>%

  ## calculate the height of the bars
  group_by(question) %>%
  summarise(score = length(response)/nparticipant) %>%

  ## Plot
  ggplot(aes(x=question, y=score)) +
  geom_bar(stat = "identity")
f%
过滤器(响应!=“”)%>%
##计算钢筋的高度
组别(问题)%>%
总结(分数=长度(反应)/参与者)%>%
##密谋
ggplot(aes(x=问题,y=分数))+
几何图形栏(stat=“identity”)

我想你想要这样的东西(带堆叠条形图的比例):

参与者Q1\u 1 Q1\u 2 Q1\u 3
1 a c
2 a c
3 c b c
4 b d
#确保所有问题列具有相同的因子水平,忽略空白
(我在2:4中){

df[,i]我想你想要这样的东西(带堆叠条形图的比例):

参与者Q1\u 1 Q1\u 2 Q1\u 3
1 a c
2 a c
3 c b c
4 b d
#确保所有问题列具有相同的因子水平,忽略空白
(我在2:4中){
df[,i]