0

I am trying to create Grouped Likert Plot using plot_liker command under sjPlot in R. Tried to execute the following command:


library(sjPlot)
library(ggplot2)
library(readxl)


mydf1<- read_excel("C:\\Users\\HP\\Desktop\\aa\\source_teacher.xlsx")
mydf1<- data.frame(mydf1)

mydf1$Domain <- factor(mydf1$Domain)

legend_labels <- c("1 Never", "2 Sometime", "3 Often", "4 Very Often") # Plotting with custom legend


p <- plot_likert(mydf1[1:4], groups = mydf1$Domain,
                 catcount = 4,
                 legend.labels = legend_labels)

p + theme(
  axis.text.y = element_text(color = "black", size = 14),
  legend.position = "bottom"
) 

It gives an error:

Error: Length of groups has to equal the number of items: ncol(items) != length(groups).

The data set is

Online Journals Online Databases E-books Others Domain
4 2 1 4 Teacher
3 3 4 2 Teacher
2 1 4 3 Student
1 3 2 4 Student

Require a grouped likert plot by grouping 'Teacher' and 'Student'.

2 Answers 2

0

From my understanding of the docs this is not possible with sjPlot::plot_likert, e.g. the groups= parameter is meant to group the items or questions. That's why it should be a vector equal to the number of items (For the provided example data where the number of rows is equal to the number of item columns aka 4 your code will actually work).

But one option to get a likert plot for each Domain category would be to switch to ggstats::gglikert:

library(tidyverse)
library(ggstats)

data <- tibble(
  `Online Journals` = c(4, 3, 2, 1),
  `Online Databases` = c(2, 3, 1, 3),
  `E-books` = c(1, 4, 4, 2),
  `Others` = c(4, 2, 3, 4),
  Domain = c("Teacher", "Teacher", "Student", "Student")
)

legend_labels <- c("1 Never", "2 Sometime", "3 Often", "4 Very Often")

data |>
  mutate(across(-Domain, ~ factor(.x, levels = 1:4, labels = legend_labels))) |>
  gglikert(
    -Domain,
    facet_rows = vars(Domain),
    variable_labels = legend_labels,
    labels_size = 3
  )

0

To use the plot_likert function with groups, the data has to be structured such that the groups are in different columns (items). You can try the following:

p <- plot_likert(data.frame(mydf1[1:2,1:4], 
                            mydf1[3:4,1:4], check.names = FALSE), 
                 groups = c(rep(1,4), rep(2,4)),
                 groups.titles = c("Teachers", "Students"),
                 catcount = 4,
                 legend.labels = legend_labels)
p

enter image description here

If your actual data is larger, use dplyr::filter or subset to do a similar selection of the rows instead of indexing.


mydf1 <- structure(list(`Online Journals` = c(4, 3, 2, 1), `Online Databases` = c(2, 
3, 1, 3), Ebooks = c(1, 4, 4, 2), Others = c(4, 2, 3, 4), Domain = structure(c(2L, 
2L, 1L, 1L), levels = c("Student", "Teacher"), class = "factor")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.