移除Likert图中的白边距。

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

Remove white side margin in a Likert plot

问题

l24g <- likert(items24[,1:2], grouping=pisaitems$CNT, plot.margin = unit(c(1, 1, 1, 1), "cm"))
plot(l24g, plot.percent.low = FALSE, plot.percent.high = FALSE)
英文:

I am a beginner with R, and I have to analyze the results of a survey including Likert scales. I would like to have multiple stacked histograms to visually compare the answers to multiple Likert questions. After no succeeding with ggplot only, I tried to use the likert package which seem to work fine (except that it does not use the columns labels), but I would like to adjust the white margin on the sides of the plot.

Here is an example (coming from here):

library(likert)
data(pisaitems)

items24 &lt;- pisaitems[,substr(names(pisaitems), 1,5) == &#39;ST24Q&#39;]

items24 &lt;- rename(items24, c(
            ST24Q01=&quot;I read only if I have to.&quot;,
            ST24Q02=&quot;Reading is one of my favorite hobbies.&quot;,
            ST24Q03=&quot;I like talking about books with other people.&quot;,
            ST24Q04=&quot;I find it hard to finish books.&quot;,
            ST24Q05=&quot;I feel happy if I receive a book as a present.&quot;,
            ST24Q06=&quot;For me, reading is a waste of time.&quot;,
            ST24Q07=&quot;I enjoy going to a bookstore or a library.&quot;,
            ST24Q08=&quot;I read only to get information that I need.&quot;,
            ST24Q09=&quot;I cannot sit still and read for more than a few minutes.&quot;,
            ST24Q10=&quot;I like to express my opinions about books I have read.&quot;,
            ST24Q11=&quot;I like to exchange books with my friends.&quot;))

l24g &lt;- likert(items24[,1:2], grouping=pisaitems$CNT)
plot(l24g)

移除Likert图中的白边距。

There are large white margins on both sides of the plot that I would like to reduce (or completely remove when I disable low or high percentages). To be explicit, it's not about the edges of the image, but the white spaces between the grey frame and the actual histogram (where there are the percentages). Also the percents on the left side seem to be "bonded" to the plot, which is not very aesthetic.

It would be perfect if I could get a rendering close to this one.

移除Likert图中的白边距。

So, how can I adjust the position of the high/low percents and/or of the white margins to save some place for the histograms?

PS: I apologize for my english mistakes...

答案1

得分: 0

如果您想要这种类型的图,实际上不需要使用likert包。只需将数据转换为长格式,汇总数据并绘制为条形图:

library(tidyverse)

pivot_longer(items24, everything()) %>%
  group_by(name) %>%
  count(value) %>%
  filter(!is.na(value)) %>%
  mutate(percentage = n/sum(n)) %>%
  ggplot(aes(percentage, name, fill = value)) +
  geom_col(position = position_stack(reverse = TRUE)) +
  geom_text(aes(label = scales::percent(percentage, 1), group = value),
            position = position_fill(vjust = 0.5, reverse = TRUE),
            color = 'white', fontface = 2) +
  scale_fill_manual(values = c('navy', 'deepskyblue4', 
                               'deepskyblue2', 'lightblue')) +
  theme_bw() +
  coord_cartesian(expand = FALSE) +
  scale_x_continuous(labels = scales::percent) +
  labs(fill = NULL, x = NULL, y = NULL) +
  theme(legend.position = 'bottom')

移除Likert图中的白边距。

英文:

If you want this kind of plot, you really don't need the likert package at all. Just pivot into long format, summarize the data and draw it as a bar graph:

library(tidyverse)

pivot_longer(items24, everything()) %&gt;%
 group_by(name) %&gt;%
  count(value) %&gt;%
  filter(!is.na(value)) %&gt;%
  mutate(percentage = n/sum(n)) %&gt;%
  ggplot(aes(percentage, name, fill = value)) +
  geom_col(position = position_stack(reverse = TRUE)) +
  geom_text(aes(label = scales::percent(percentage, 1), group = value),
            position = position_fill(vjust = 0.5, reverse = TRUE),
            color = &#39;white&#39;, fontface = 2) +
  scale_fill_manual(values = c(&#39;navy&#39;, &#39;deepskyblue4&#39;, 
                               &#39;deepskyblue2&#39;, &#39;lightblue&#39;)) +
  theme_bw() +
  coord_cartesian(expand = FALSE) +
  scale_x_continuous(labels = scales::percent) +
  labs(fill = NULL, x = NULL, y = NULL) +
  theme(legend.position = &#39;bottom&#39;)

移除Likert图中的白边距。

huangapple
  • 本文由 发表于 2023年2月23日 22:18:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546053.html
匿名

发表评论

匿名网友

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

确定