0

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

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)

一个帮助。

4

1 回答 1

1

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

另外请注意,它plot_likert仅适用于偶数个类别(请参阅 参考资料?plot_likert)。但是您通过 option 设置了中性类别 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)

于 2021-09-03T13:39:57.870 回答