Search code examples
rggplot2likert

Why are the bars inverted on the x axis when using 100% stacked bar plots with grouping in the Likert package in R?


Grouped likert scale using Likert package in R, results in plot with reversed scale. I have series of likert variables (strongly disagree-strongly agree) and a grouping variable from a survey. I am using Likert package in R and everything goes well until I want to plot one of those likert variables grouped by a grouping variable in a 100% stacked bars plot. In likert package to plot 100% stacked bar you can use the option centered=FALSE. This option works fine when plotting without grouping.

In the following code you can see the problem arising in the 'L3' plot, in that the bars are inverted on the x axis compared to 'L2'. The blue color should be on the right and the yellows on the left. Note also that the percentages for neutral are not aligned, but they would be aligned if the bars were in the correct sense

library(likert)
library(tidyverse)

n <- 3000

set.seed(123)

# Dataframe where Q1a,b,c are the likert variables with levels 1,2,3,4,5 and 3 is the centre (neutral answer)
some_made_up_data <- data.frame(
  Q1a = as.factor(sample(c(1,2,3,4,5), n, replace = TRUE, prob = c(.03,.07,.2,.4, .3))),
  Q1b = as.factor(sample(c(1,2,3,4,5), n, replace = TRUE, prob = c(.02,.1,.2,.3, .3))),
  Q1c = as.factor(sample(c(1,2,3,4,5), n, replace = TRUE, prob = c(.05,.2,.2,.4, .2))),
  group = as.factor(sample(c("g1", "g2", "g3", "g4", "g5"), n, replace = TRUE))
)

# Simple Plot 100% stacked bars
L1<-likert(some_made_up_data[,-4]) %>% 
  plot(type="bar",
       centered=F
  )

# Plot with grouping, standard divergent bars
L2<-likert(some_made_up_data[,1,drop=FALSE],grouping = some_made_up_data$group) %>% 
  plot(type="bar",
       centered=T
  )


# Plot with grouping, 100% stacked bars
## Here I am subsetting the dataframe to use only the first of the likert variables and compare distribution s across groups

L3<-likert(some_made_up_data[,1,drop=FALSE],grouping = some_made_up_data$group) %>% 
  plot(type="bar",
       centered=F
  )

# View plots
L1
L2
L3

I tried to play with the options of likert.options in the likert package, but without successAlso I cannot find any solution by searching online for simlar problem. I am expecting the bars representing the frequencies of the likert levels to be in the correct sense, lower likert values ont he left and high likert values on the right as in 'L2' plot.

Any suggestion is very appreciated. Thank you.


Solution

  • There might be a cleaner option which I haven't found in the docs but one option to fix the order of the stack would be to manipulate the ggplot object returned by likert.bar.plot directly and reverse the order of the stack by setting the position for the geom_col layer aka layer no. 1 to position_stack(reverse = TRUE):

    library(likert)
    #> Loading required package: ggplot2
    #> Loading required package: xtable
    library(tidyverse)
    
    n <- 3000
    
    set.seed(123)
    
    some_made_up_data <- data.frame(
      Q1a = as.factor(sample(c(1, 2, 3, 4, 5), n, replace = TRUE, prob = c(.03, .07, .2, .4, .3))),
      Q1b = as.factor(sample(c(1, 2, 3, 4, 5), n, replace = TRUE, prob = c(.02, .1, .2, .3, .3))),
      Q1c = as.factor(sample(c(1, 2, 3, 4, 5), n, replace = TRUE, prob = c(.05, .2, .2, .4, .2))),
      group = as.factor(sample(c("g1", "g2", "g3", "g4", "g5"), n, replace = TRUE))
    )
    
    L3 <- likert(some_made_up_data[, 1, drop = FALSE], grouping = some_made_up_data$group) %>%
      plot(
        type = "bar",
        centered = F
      )
    
    L3$layers[[1]]$position <- position_stack(reverse = TRUE)
    
    L3