rdataframeggplot2likert

Wrap text in legend in ggplot2 from likert package in R


i have a data frame in R called df which is a subset of pisaitems data of likert package.

The problem i face is that when i plot the result of the likert function i want to wrap the text of the categories that start with "Strongly" and the agree or disagree to appear below the word Strongly.How can i do that in this likert package setting using ggplot2 ?

dput(df)
structure(list(ST24Q01 = structure(c(2L, 2L, 4L, 2L, 1L, 2L, 
2L, 2L, 2L, 4L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 
4L, 2L, 2L, 2L, 1L, 2L, 1L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L), levels = c("Strongly disagree", 
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q02 = structure(c(4L, 
1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 
4L, 4L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 
4L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q03 = structure(c(4L, 1L, 1L, 2L, 1L, 
1L, 2L, 1L, 2L, 1L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 4L), levels = c("Strongly disagree", 
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q04 = structure(c(1L, 
4L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 1L, 2L, 1L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 
1L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q05 = structure(c(4L, 1L, 1L, 2L, 1L, 
1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 
1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 4L), levels = c("Strongly disagree", 
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q06 = structure(c(1L, 
2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
1L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q07 = structure(c(2L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 
4L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 1L, 4L), levels = c("Strongly disagree", 
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q08 = structure(c(2L, 
2L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 
2L, 2L, 1L, 1L, 4L, 4L, 2L, 4L, 1L, 2L, 1L, 2L, 4L, 2L, 2L, 2L, 
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 
1L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q09 = structure(c(1L, 4L, 2L, 1L, 2L, 
4L, 2L, 1L, 2L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 
2L, 2L, 2L, 4L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 
2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L), levels = c("Strongly disagree", 
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q10 = structure(c(2L, 
1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 
4L, 4L, 4L, 4L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 
4L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q11 = structure(c(2L, 1L, 1L, 2L, 1L, 
1L, 2L, 2L, 2L, 1L, 4L, 1L, 1L, 4L, 2L, 2L, 4L, 4L, 4L, 2L, 2L, 
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 1L, 2L, 1L, 4L), levels = c("Strongly disagree", 
"Disagree", "Agree", "Strongly agree"), class = "factor")), row.names = 68038:68087, class = "data.frame")
df
library(tidyverse)
library(likert)
likert1<-likert::likert(df)

plot(likert1 , 
     centered = TRUE, center = 3 , 
     ordered = FALSE , 
     include.center=TRUE ,  
     low.color = 'red', 
     neutral.color = 'lightblue' , 
     high.color='green' ,facet=TRUE , 
     legend.position = "bottom" ,
     pch = 0.7 , 
     lcolor = "Black", wrap = 70) + 
  theme(axis.text.y = element_text(colour="black", size="11", hjust =0)) + 
  theme(legend.title=element_blank())

i receive :

enter image description here


Solution

  • You could wrap the labels by setting the labels of your factor variables and by replacing the spaces by line breaks.

    Note: A second option would be to wrap the labels via the labels argument of scale_fill_xxx but that would override the colors applied by likert and hence would require some additional effort to get the right colors.

    library(likert)
    #> Loading required package: ggplot2
    #> Loading required package: xtable
    
    df[] <- lapply(df, \(x) {
      factor(x,
        levels = levels(df$ST24Q01),
        labels = gsub("\\s", "\n", levels(df$ST24Q01))
      )
    })
    
    likert1 <- likert::likert(df)
    
    plot(likert1,
      centered = TRUE, center = 3,
      ordered = FALSE,
      include.center = TRUE,
      low.color = "red",
      neutral.color = "lightblue",
      high.color = "green", facet = TRUE,
      legend.position = "bottom",
      pch = 0.7,
      lcolor = "Black", wrap = 70
    ) +
      theme(
        axis.text.y = element_text(
          colour = "black", size = "11", hjust = 0
        ),
        legend.title = element_blank()
      )