Search code examples
rplotsjplotlikert

Plotting likert scales with plot_likert function from sjPlot


I have likert scale data in character format and want to plot it using plot_likert function from sjPlot R package.

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)

However, plot_likert works for numeric data.

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

df2

plot_likert(df2)

An help.


Solution

  • To plot your data you have to convert your characters to ordered factors before passing it to plot_likert. Otherwise, how should plot_likert know, how to order your categories.

    Additionally note that plot_likert works only for an even number of categories (see ?plot_likert). But you you set a neutral category via 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)