英文:
Why are the bars inverted on the x axis when using 100% stacked bar plots with grouping in the Likert package in R?
问题
在使用R中的Likert包绘制分组的Likert比例刻度时,结果是一个反向刻度的图形。
我有一系列的Likert变量(强烈不同意-强烈同意)和来自一项调查的分组变量。我在R中使用Likert包,一切都进行得很顺利,直到我想要绘制其中一个Likert变量,以分组变量在100%堆叠柱状图中进行分组。
在Likert包中,要绘制100%堆叠柱状图,您可以使用选项centered=FALSE。这个选项在没有分组的情况下绘制时效果很好。
在下面的代码中,您可以看到问题出现在'L3'图中,其中与'L2'相比,柱状图在x轴上是倒置的。蓝色应该在右边,黄色在左边。还要注意,中性的百分比没有对齐,但如果柱状图是正确的话,它们将对齐。
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
我尝试了在Likert包中使用likert.options选项,但没有成功,也无法通过在线搜索类似问题来找到任何解决方案。
我期望表示Likert水平频率的柱状图是正确的,较低的Likert值在左边,较高的Likert值在右边,就像'L2'图中一样。
非常感谢您的任何建议。谢谢。
英文:
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.
答案1
得分: 1
可能有一个更干净的选项,我在文档中没有找到,但是修复堆栈顺序的一个选项是直接操作likert.bar.plot
返回的ggplot
对象,并通过将geom_col
层(即第1层)的position
设置为position_stack(reverse = TRUE)
来反转堆栈的顺序:
library(likert)
#> 加载所需的包: ggplot2
#> 加载所需的包: 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
<!-- -->
英文:
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
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论