Search code examples
ggplot2axis-labelslikert

Likert package barplot is adding some groups to the plot twice, how can I remove them -- what am I doing wrong?


I am trying to plot the following Likert item:

    structure(list(`Likelihood of attending more frequently` = structure(c(4L, 
4L, 4L, 3L, 4L, 4L, 3L, 4L, 5L, 4L, 4L, 4L, 4L, 5L, 3L, 5L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 5L, 4L, 3L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 5L, 4L, 4L, 4L, 3L, 
4L, 5L, 3L, 3L, 2L, 4L, 4L, 3L, 4L, 1L, 3L, 3L, 4L, 5L, 4L, 4L, 
4L, 4L, 4L, 3L, 4L, 4L, 5L, 4L, 2L, 5L, 3L, 2L, 3L, 3L, 5L, 4L, 
4L, 2L, 3L, 2L, 4L, 5L, 3L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 3L, 4L, 
4L, 4L, 5L, 3L, 3L, 4L, 3L, 4L, 3L, 3L, 5L, 5L, 4L, 4L, 5L, 2L, 
1L, 4L, 4L, 4L, 1L, 4L, 3L, 2L, 4L, 4L, 4L, 4L, 5L, 4L, 5L, 4L, 
5L, 4L, 2L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 3L, 
3L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 5L, 3L, 2L, 4L, 5L, 5L, 1L, 5L, 
2L, 1L, 5L, 4L, 1L, 4L, 5L, 4L, 2L, 4L, 4L, 4L, 5L, 3L, 2L, 5L, 
5L, 5L, 4L), .Label = c("Highly unlikely", "Somewhat unlikely", 
"Neither likely nor unlikely", "Somewhat likely", "Highly likely"
), class = "factor"), Reason = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Class times conflict with my schedule", 
"I am not interested in the content offered", "I don't have time", 
"I don't have transportation", "I don't know where to find information on classes, programs, and events", 
"Other", "There are not classes near me"), class = "factor")), row.names = c(NA, 
-180L), class = "data.frame")

When I check the factor levels of rhe group items it displays 7 , however, the likert.bar.plot has 9 groups as 2 are duplicated.

like<-likert(freq[, c(1), drop=FALSE], grouping = freq$Reason)
likert.bar.plot(like,wrap.grouping = 40)

I have checked to see if the text strings are different, but they are exactly the same. Any idea what could be causing this?

enter image description here


Solution

  • As I already mentioned in my comment this looks like a bug to me. Both from the source code of likert.bar.plot and from the image it looks as if for the bars the unwrapped group labels are used, while for the percentage labels the wrapped ones are used. Hence we end up with duplicated groups.

    However, as a workaround you could wrap the group labels manually before passing to likert. To make this work you also have to set the same width for the wrap.grouping argument:

    library(likert)
    
    levels(freq$Reason) <- stringr::str_wrap(levels(freq$Reason), width = 40)
    like <- likert(freq[, 1, drop = FALSE], grouping = freq$Reason)
    likert.bar.plot(like, wrap.grouping = 40)