【问题标题】:Plotting likert scales with plot_likert function from sjPlot使用 sjPlot 中的 plot_likert 函数绘制李克特尺度
【发布时间】:2021-09-03 13:10:29
【问题描述】:

我有字符格式的李克特比例数据,并想使用 sjPlot R 包中的 plot_likert 函数绘制它。

df1 <-
  data.frame(
  matrix(
    data = sample(x = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"), size = 500, replace = TRUE),
    ncol = 5
    )
  )
    
head(df1)
                 X1                X2                X3                X4
1           Neutral           Neutral Strongly Disagree    Strongly Agree
2          Disagree          Disagree    Strongly Agree             Agree
3           Neutral    Strongly Agree Strongly Disagree             Agree
4           Neutral    Strongly Agree             Agree Strongly Disagree
5           Neutral           Neutral          Disagree Strongly Disagree
6 Strongly Disagree Strongly Disagree             Agree Strongly Disagree
                 X5
1          Disagree
2 Strongly Disagree
3 Strongly Disagree
4           Neutral
5 Strongly Disagree
6             Agree

library(sjPlot)

plot_likert(df1)

    Error: Can't coerce element 1 from a character to a double
In addition: There were 18 warnings (use warnings() to see them)

但是,plot_likert 适用于数字数据。

df2 <-
  data.frame(
  matrix(
    data = sample(x = 1:5, size = 500, replace = TRUE),
    ncol = 5
    )
  )

df2

plot_likert(df2)

帮助。

【问题讨论】:

    标签: r plot sjplot likert


    【解决方案1】:

    要绘制您的数据,您必须先将您的characters 转换为ordered factors,然后再将其传递给plot_likert。不然plot_likert怎么知道,怎么给你的分类排序。

    另外请注意,plot_likert 仅适用于偶数个类别(请参阅?plot_likert)。但是您通过选项cat.neutral 设置了一个中性类别。

    library(sjPlot)
    
    df1 <-
      data.frame(
        matrix(
          data = sample(x = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"), size = 500, replace = TRUE),
          ncol = 5
        )
      )
    
    df1 <- dplyr::mutate_all(df1, ~ ordered(., levels = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")))
    
    plot_likert(df1, cat.neutral = 3)
    

    【讨论】:

    • 感谢@stefan 非常有用的回答。
    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多