【问题标题】:R multiple choice questionnaire data to ggplotR多项选择问卷数据到ggplot
【发布时间】:2016-10-04 14:41:20
【问题描述】:

我有一个 Qualtrics 多项选择题,我想用它在 R 中创建图表。我的数据经过整理,以便您可以为每个问题回答多个答案。例如,参与者 1 选择了多项选择答案 1 (Q1_1) 和 3 (Q1_3)。我想在一个条形图中折叠所有答案选项,每个多重响应选项(Q1_1:Q1_3)一个条形除以回答此问题的受访者数量(在本例中为 3)。

df <- 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")

我想使用 ggplot2 并且可能通过 Q1_1 进行某种循环:Q1_3?

【问题讨论】:

    标签: r ggplot2 survey qualtrics


    【解决方案1】:

    也许这就是你想要的

    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")
    

    【讨论】:

      【解决方案2】:

      这是使用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() 
      

      注意:如果您有更多与其他问题相关的列(“Q2_1”、“Q2_2”...),可能会更复杂。在这种情况下,融合每个问题的数据可能是一个解决方案。

      【讨论】:

        【解决方案3】:

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

          Participant Q1_1 Q1_2 Q1_3
        1           1    a         c
        2           2    a    a    c
        3           3    c    b    c
        4           4         b    d
        
        # ensure that all question columns have the same factor levels, ignore blanks
        for (i in 2:4) {
           df[,i] <- factor(df[,i], levels = c(letters[1:4]))
        }
        
        tdf <- as.data.frame(sapply(df[2:4], function(x)table(x)/sum(table(x))))
        tdf$choice <- rownames(tdf)
        tdf <- melt(tdf, id='choice')
        
        ggplot(tdf, aes(variable, value, fill=choice)) + 
               geom_bar(stat='identity') + 
               xlab('Questions') + 
               ylab('Proportion of Choice')
        

        【讨论】:

          猜你喜欢
          • 2018-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-06
          • 2020-06-03
          相关资源
          最近更新 更多