【问题标题】:Displaying answers on ranking question in R在 R 中显示排名问题的答案
【发布时间】:2022-08-19 19:53:00
【问题描述】:

我有以下变量,它们是一个排名问题的结果。在这个问题上,参与者得到了列出的 7 个动机并应该对它们进行排名。这里,值 1 表示参与者将动机放在位置 1,值 7 表示他将动机放在最后位置。排名通过这些变量的数字(数字 1 到 7)表示:

\'data.frame\':   25 obs. of  8 variables:
 $ id                       : num  8 9 10 11 12 13 14 15 16 17 ...
 $ motivation_quantity      : num  NA 3 1 NA 3 NA NA NA 1 NA ...
 $ motivation_quality       : num  NA 1 6 NA 3 NA NA NA 3 NA ...
 $ motivation_timesaving    : num  NA 6 4 NA 2 NA NA NA 5 NA ...
 $ motivation_contribution  : num  NA 4 2 NA 1 NA NA NA 2 NA ...
 $ motivation_alternativelms: num  NA 5 3 NA 6 NA NA NA 7 NA ...
 $ motivation_inspiration   : num  NA 2 7 NA 4 NA NA NA 4 NA ...
 $ motivation_budget        : num  NA 7 5 NA 7 NA NA NA 6 NA ...

我现在要做的是计算和可视化排名问题的结果(即可视化动机的结果)。由于我很长时间没有使用 R,我不确定如何最好地做到这一点。

我可以想象的一种方法是首先计算前 3 个答案(这是最常在参与者中排名“1”、“2”和“3”的动机。 如果有人能帮忙做这件事,甚至展示如何分析和可视化我的数据的更好方法,我将不胜感激。

我最初有一个微软表单的可视化,但这个可视化在一夜之间被一个错误破坏了。它看起来像这样:

    标签: r data-visualization ranking survey


    【解决方案1】:

    这些变量由 RStudio 定义为数字(在统计术语中它指的是连续变量)。然后,目标是将它们转换为分类变量(在 RStudio 中称为因子)。

    让我们开始工作吧:

    
    library(dplyr)
    library(tidyr)
    
    # lets us first convert the id column into integers so we can apply mutate_if on the other numeric factors and convert all of them into factors (categorical variables), we shall name your dataframe (df)
    
    df$id <- as.integer(df$id)
    
    
    # and now let's apply mutate_if to convert all the other variables (numeric) into factors (categorical variables).
    
    df <- df %>% mutate_if(is.numeric,factor,
                         levels = 1:7)
    
    # I guess in your case that would be all, but if you wanted the content of the dataframe to be position_1, position_2 ...position_7, we just add labels like this :
    
    df <- df %>% mutate_if(is.numeric,factor,
                         levels = 1:7,
                         labels = paste(rep("position",7),1:7,sep="_"))
    
    
    # For the visualisation now, we need to use the function gather in order to convert the df dataframe into a two column dataframe (and keeping the id column), we shall name this new dataframe df1
    
    df1 <- df %>% gather(key=Questions, value=Answers, motivation_quantity:motivation_budget,-id  )
    
    
    # the df1 dataframe now includes three columns : the id column - the Questions columns - the Answers column. 
    # we can now apply the ggplot function on the new dataframe for the visualisation
    
    # first the colours
    
    colours <- c("firebrick4","firebrick3", "firebrick1", "gray70", "blue", "blue3" ,"darkblue") 
    
    
    # ATTENTION since there are NAs in your dataframe, either you can recode them as zeros or delete them (for the visualisation) using the subset function within the ggplot function as follows :
    
    ggplot(subset(df1,!is.na(Answers)))+
      aes(x=Questions,fill=Answers)+
      geom_bar()+
      coord_flip()+
      scale_fill_manual(values = clrs7) + 
      ylab("position_levels")
    
    # of course you can enter many modifications into the visualisation but in total I think that's what you need.
    
    
    
    

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      相关资源
      最近更新 更多