Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bars inverted on the x axis when using 100% stacked bar plots with grouping #138

Open
aauro opened this issue Jun 2, 2023 · 0 comments
Open

Comments

@aauro
Copy link

aauro commented Jun 2, 2023

Grouped likert scale using Likert package in R, results in plot with reversed scale. I have a 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`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant