R 中 ggplot2 的 Likert 图具有不同的排名级别并显示百分比

问题描述 投票:0回答:1

我在 R 中有一个名为 df 的数据框,如下所示:

  df
# A tibble: 90 × 3
# Groups:   item [18]
   item  Response          Percentage
   <chr> <fct>                  <dbl>
 1 A     Very Dissatisfied        0  
 2 A     Dissatisfied             0  
 3 A     Average                 33.3
 4 A     Satisfied               11.1
 5 A     Very Satisfied          55.6
 6 B     Very Dissatisfied        0  
 7 B     Dissatisfied             0  
 8 B     Average                 44.4
 9 B     Satisfied                0  
10 B     Very Satisfied          55.6
# ℹ 80 more rows
# ℹ Use `print(n = ...)` to see more rows

响应栏是李克特量表栏,有 5 个级别。用 ggplot2 绘制它



# Creating a mapping of responses to numeric values
response_mapping <- c("Very Dissatisfied" = 1,
                      "Dissatisfied" = 2,
                      "Average" = 3,
                      "Satisfied" = 4,
                      "Very Satisfied" = 5)

# Applying the mapping and calculate the sign
data_f_sum <- df %>% 
  ungroup() %>% 
  mutate(res.sgn = sign(response_mapping[as.character(Response)] - 3)) %>% 
  summarise(sum.prcnt = sum(Percentage),
            .by = c(item, res.sgn))
likert_levels =  c("Very Dissatisfied", 
                   "Dissatisfied" ,
                   "Average" ,
                   "Satisfied", 
                   "Very Satisfied")

df = df%>%
  mutate(Response = factor(Response , levels = likert_levels))

ggplot(data = df, 
       aes(Percentage, item, fill = Response)) +
  geom_col(position = position_likert()) +
  scale_x_continuous(breaks = seq(-1, 1, 0.5),
                     labels = ggstats::label_percent_abs()) +
  geom_label(data = data_f_sum,
             aes(label = sprintf("%.1f", sum.prcnt), y = item, x = res.sgn * 0.5),
             alpha = 0.3, inherit.aes = FALSE) +
  scale_fill_brewer(type = "div", palette = "RdYlGn") +
  theme_bw()

我收到以下情节(图片)

我想解决我在这里遇到的一些问题,我需要一些帮助:

  1. 我想固定中间水平的平均水平,从0%开始。

  2. 我想在平均水平的每个项目的中间显示平均水平的百分比。在每个项目的左侧显示两个较低级别的百分比总和(即非常不满意和满意),在右侧显示两个较高级别的百分比总和(即满意和非常满意)。像这样:

有人可以帮我解决这些问题吗?

数据在这里:

 structure(list(item = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "E", 
"E", "E", "E", "E", "F", "F", "F", "F", "F", "G", "G", "G", "G", 
"G", "H", "H", "H", "H", "H", "I", "I", "I", "I", "I", "J", "J", 
"J", "J", "J", "K", "K", "K", "K", "K", "L", "L", "L", "L", "L", 
"M", "M", "M", "M", "M", "N", "N", "N", "N", "N", "O", "O", "O", 
"O", "O", "P", "P", "P", "P", "P", "Q", "Q", "Q", "Q", "Q", "R", 
"R", "R", "R", "R"), Response = structure(c(4L, 2L, 1L, 3L, 5L, 
4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 
2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 
1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 
3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 
5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 
4L, 2L, 1L, 3L, 5L), levels = c("Average", "Dissatisfied", "Satisfied", 
"Very Dissatisfied", "Very Satisfied"), class = "factor"), Percentage = c(0, 
0, 33.3, 11.1, 55.6, 0, 0, 44.4, 0, 55.6, 0, 0, 22.2, 33.3, 44.4, 
0, 0, 33.3, 11.1, 55.6, 0, 22.2, 11.1, 11.1, 55.6, 0, 0, 44.4, 
11.1, 44.4, 0, 0, 11.1, 33.3, 55.6, 0, 0, 33.3, 22.2, 44.4, 0, 
0, 11.1, 33.3, 55.6, 0, 0, 22.2, 22.2, 55.6, 0, 0, 11.1, 11.1, 
77.8, 0, 0, 11.1, 33.3, 55.6, 0, 0, 33.3, 0, 66.7, 0, 0, 33.3, 
11.1, 55.6, 0, 11.1, 0, 33.3, 55.6, 0, 0, 22.2, 22.2, 55.6, 0, 
22.2, 22.2, 11.1, 44.4, 0, 11.1, 22.2, 11.1, 55.6)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -90L), groups = structure(list(
    item = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "K", "L", "M", "N", "O", "P", "Q", "R"), .rows = structure(list(
        1:5, 6:10, 11:15, 16:20, 21:25, 26:30, 31:35, 36:40, 
        41:45, 46:50, 51:55, 56:60, 61:65, 66:70, 71:75, 76:80, 
        81:85, 86:90), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -18L), .drop = TRUE))

r ggplot2 likert
1个回答
0
投票

您可以使用

coord_cartesian
通过对 x 轴硬设置限制来使李克特量表居中,这样您的绘图实际上从 -100% 到 100%,而不是调整到数据范围,并且您可以将标签放在只需使用 -1 或 1 作为 x 值即可绘制图的边缘:

library(ggplot2)
library(ggstats)

ggplot(data = df, 
       aes(Percentage, item, fill = Response)) +
  geom_col(position = position_likert()) +
  scale_x_continuous(#breaks = seq(-1, 1, 0.5),
                      
                     labels = ggstats::label_percent_abs()) +
  geom_label(data = data_f_sum,
             aes(label = sprintf("%.1f", sum.prcnt), y = item, x = res.sgn),
             alpha = 0.3, inherit.aes = FALSE) +
  coord_cartesian(xlim = c(-1, 1)) +
  scale_fill_brewer(type = "div", palette = "RdYlGn") +
  theme_bw()

© www.soinside.com 2019 - 2024. All rights reserved.