为什么在R中使用Likert包的100%堆叠条形图时,x轴上的条形被反转?

huangapple go评论65阅读模式
英文:

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 &lt;- 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 &lt;- 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(&quot;g1&quot;, &quot;g2&quot;, &quot;g3&quot;, &quot;g4&quot;, &quot;g5&quot;), n, replace = TRUE))
)

# Simple Plot 100% stacked bars
L1&lt;-likert(some_made_up_data[,-4]) %&gt;% 
  plot(type=&quot;bar&quot;,
       centered=F
  )

# Plot with grouping, standard divergent bars
L2&lt;-likert(some_made_up_data[,1,drop=FALSE],grouping = some_made_up_data$group) %&gt;% 
  plot(type=&quot;bar&quot;,
       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&lt;-likert(some_made_up_data[,1,drop=FALSE],grouping = some_made_up_data$group) %&gt;% 
  plot(type=&quot;bar&quot;,
       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)
#&gt; 加载所需的包: ggplot2
#&gt; 加载所需的包: xtable
library(tidyverse)

n &lt;- 3000

set.seed(123)

some_made_up_data &lt;- 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(&quot;g1&quot;, &quot;g2&quot;, &quot;g3&quot;, &quot;g4&quot;, &quot;g5&quot;), n, replace = TRUE))
)

L3 &lt;- likert(some_made_up_data[, 1, drop = FALSE], grouping = some_made_up_data$group) %&gt;%
  plot(
    type = &quot;bar&quot;,
    centered = F
  )

L3$layers[[1]]$position &lt;- position_stack(reverse = TRUE)

L3

为什么在R中使用Likert包的100%堆叠条形图时,x轴上的条形被反转?<!-- -->

英文:

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)
#&gt; Loading required package: ggplot2
#&gt; Loading required package: xtable
library(tidyverse)

n &lt;- 3000

set.seed(123)

some_made_up_data &lt;- 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(&quot;g1&quot;, &quot;g2&quot;, &quot;g3&quot;, &quot;g4&quot;, &quot;g5&quot;), n, replace = TRUE))
)

L3 &lt;- likert(some_made_up_data[, 1, drop = FALSE], grouping = some_made_up_data$group) %&gt;%
  plot(
    type = &quot;bar&quot;,
    centered = F
  )

L3$layers[[1]]$position &lt;- position_stack(reverse = TRUE)

L3

为什么在R中使用Likert包的100%堆叠条形图时,x轴上的条形被反转?<!-- -->

huangapple
  • 本文由 发表于 2023年6月2日 06:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定