我有一个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

响应列为Likert量表列,有5个级别.用gggplot 2绘制它



# 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()

我收到以下情节(图片)

enter image description here

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

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

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

enter image description here

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

数据在这里:

 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))

推荐答案

您可以通过x轴的硬设置限制(使用coord_cartesian)将likert比例居中,以便您的图实际上从-coord_cartesian%变为coord_cartesian%,而不是根据数据范围进行调整,并且您可以仅通过使用-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()

enter image description here

R相关问答推荐

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

编码变量a、b、c以匹配来自另一个数据点的变量x

如何使用stat_extract_all正确提取我的目标值?

名称输出pmap on tible

任意列的欧几里得距离

如何使用R对每组变量进行随机化?

使用tidy—select创建一个新的带有mutate的摘要变量

如何优化向量的以下条件赋值?

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

合并后返回列表的数据帧列表

条形图顶部与其错误条形图不对齐

如何从向量构造一系列双边公式

使用未知字符数(不受限制的最大长度)的Lookback有什么好的替代方案?

如何删除R中除数字元素以外的所有元素

防止在更新SHINY中的Reactive Value的部分内容时触发依赖事件

是否可以将线性模型的p值添加到tbl_summary中

防止正则表达式覆盖以前的语句

R预测包如何处理ARIMA(Auto.arima函数)中的缺失值

R-如何在ggplot2中显示具有不同x轴值(日期)的多行?

为什么R列名称忽略具有指定名称的向量,而只关注索引?