rplotbar-chartlikert

Re-ordering plots based on groups in HH package for R


I am analyzing pre- and post-test results from a group of healthcare workers who went through a training session. I am using the HH package for R to display results for the Likert-scale items in the pre- and post-tests. I have created a plot using likert() grouped by timing (pre- and post- training) to display the results of both tests together and illustrate the benefit of the training.

HH's likert() always displays the post-test on the top, and the pre-test on the bottom, which isn't intuitive for a reader. I want the pre-test on the top, and the post-test below.

I cannot understand the documentation to change the order of the plots based on my grouping. I think it has something to do with scales=list(y=list(relation="free")) but I can't find what I need in https://cran.r-project.org/web/packages/HH/HH.pdf.

Here is an attempt at a reproducible example. Using this example plot, let's pretend I want "Gender" to display above "Employment Sector," which is not alphabetical or in any numerical order.

library(HH)
library(tidyverse)
library("vcd")
data("ProfChal", package = "HH")

likert(Question ~ . | Subtable, data = ProfChal, as.percent = TRUE, positive.order = TRUE, 
       scales = list(y = list(relation = "free")), layout = c(1, 6))

Session info:

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] vcd_1.4-11          HH_3.1-49           likert_1.3.5        xtable_1.8-4        farver_2.1.1        lubridate_1.9.3    
 [7] forcats_1.0.0       stringr_1.5.0       dplyr_1.1.3         purrr_1.0.2         readr_2.1.4         tidyr_1.3.0        
[13] tibble_3.2.1        ggplot2_3.4.3       tidyverse_2.0.0     gridExtra_2.3       multcomp_1.4-25     TH.data_1.1-2      
[19] MASS_7.3-60         survival_3.5-5      mvtnorm_1.2-3       latticeExtra_0.6-30 lattice_0.21-8     

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0   psych_2.3.9        Rmpfr_0.9-3        fastmap_1.1.1      promises_1.2.1     digest_0.6.33     
 [7] rpart_4.1.19       timechange_0.2.0   mime_0.12          lifecycle_1.0.3    cluster_2.1.4      ellipsis_0.3.2    
[13] magrittr_2.0.3     compiler_4.3.1     rlang_1.1.1        Hmisc_5.1-1        tools_4.3.1        utf8_1.2.3        
[19] data.table_1.14.8  knitr_1.44         labeling_0.4.3     htmlwidgets_1.6.2  interp_1.1-4       mnormt_2.1.1      
[25] plyr_1.8.9         RColorBrewer_1.1-3 abind_1.4-5        withr_2.5.1        foreign_0.8-84     nnet_7.3-19       
[31] fansi_1.0.4        colorspace_2.1-0   scales_1.2.1       cli_3.6.1          rmarkdown_2.25     generics_0.1.3    
[37] rstudioapi_0.15.0  reshape2_1.4.4     tzdb_0.4.0         splines_4.3.1      parallel_4.3.1     base64enc_0.1-3   
[43] vctrs_0.6.3        Matrix_1.5-4.1     sandwich_3.0-2     hms_1.1.3          Formula_1.2-5      htmlTable_2.4.1   
[49] jpeg_0.1-10        glue_1.6.2         codetools_0.2-19   stringi_1.7.12     gtable_0.3.4       later_1.3.1       
[55] deldir_1.0-9       gmp_0.7-2          lmtest_0.9-40      munsell_0.5.0      pillar_1.9.0       htmltools_0.5.6   
[61] R6_2.5.1           evaluate_0.22      shiny_1.7.5        png_0.1-8          backports_1.4.1    leaps_3.1         
[67] httpuv_1.6.11      Rcpp_1.0.11        nlme_3.1-162       checkmate_2.2.0    xfun_0.40          zoo_1.8-12        
[73] pkgconfig_2.0.3 

Solution

  • You can achieve the desired order by making the variable that names the facets a factor and ordering the levels accordingly.

    library(HH)
    library(tidyverse)
    library("vcd")
    data("ProfChal", package = "HH")
    
    ProfChal_mod <-
      ProfChal |>
      mutate(Subtable = factor(
        Subtable,
        levels = c(
          "Gender",
          "Employment sector",
          "Race",
          "Education",
          "Attitude\ntoward\nProfessional\nRecognition"
        )
      ))
    
    likert(
      Question ~ . |
        Subtable,
      data = ProfChal_mod,
      as.percent = TRUE,
      positive.order = TRUE,
      scales = list(y = list(relation = "free")),
      layout = c(1, 6)
    )