在R中有以下数据帧,称为data_f:

> data_f
A tibble: 60 × 4
# Groups:   Year, item [12]
   Year  item  Response            Percentage
   <fct> <chr> <chr>                    <dbl>
 1 2019  A     1-Strongly disagree       20.9
 2 2019  A     2-Disagree                19.9
 3 2019  A     3-so-so                   20.3
 4 2019  A     4-Agree                   19  
 5 2019  A     5-Strongly agree          19.9
 6 2019  B     1-Strongly disagree       20.5
 7 2019  B     2-Disagree                19.9
 8 2019  B     3-so-so                   19.5
 9 2019  B     4-Agree                   21  
10 2019  B     5-Strongly agree          19.2
# ℹ 50 more rows
# ℹ Use `print(n = ...)` to see more rows

我想要用ggplot2包来绘制这个特定的图,代码如下:

data_f%>%
  ggplot(aes(Percentage,Year, fill = Response)) +
  geom_col(position = ggstats::position_likert()) +
  scale_x_continuous(labels = ggstats::label_percent_abs()) +
  scale_fill_brewer(type = "div", palette = "RdYlGn") +
  facet_wrap(~item,dir="v") +
  theme_bw()

但我想增加一些百分比.例如,我想穿过中间的条形图显示每个年份的平均值,而x轴显示100—50—0—50—100?我该如何做到这一点?

enter image description here

structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), levels = c("2019", "2020", "2022", "2023"
), class = "factor"), item = c("A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "C", "C", "C", "C", "C", "A", "A", "A", "A", 
"A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "A", "A", 
"A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", 
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", 
"C", "C"), Response = c("1-Strongly disagree", "2-Disagree", 
"3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree", "1-Strongly disagree", 
"2-Disagree", "3-so-so", "4-Agree", "5-Strongly agree"), Percentage = c(20.9, 
19.9, 20.3, 19, 19.9, 20.5, 19.9, 19.5, 21, 19.2, 20.3, 19.1, 
19.6, 20.3, 20.7, 20.1, 21, 20.1, 19, 19.9, 20.1, 20, 19.6, 18.9, 
21.4, 19.3, 20.7, 20.2, 19.7, 20.2, 20.7, 20, 17.6, 21.3, 20.4, 
19, 20.2, 20.5, 20.4, 19.9, 20.2, 19.4, 19.5, 20.3, 20.6, 19.4, 
19.9, 21.4, 19.8, 19.5, 20.7, 19.7, 18.6, 20.9, 20.1, 18.2, 20.7, 
19.7, 20.3, 21.1)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -60L), groups = structure(list(
    Year = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
    4L, 4L), levels = c("2019", "2020", "2022", "2023"), class = "factor"), 
    item = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", 
    "B", "C"), .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), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))

推荐答案

下面是使用geom_errorbar的另一种方法:

library(dplyr)
library(scales)
library(ggplot2)
library(ggstats)

data_f %>% 
  mutate(mean.resp = 
            mean((as.numeric(substr(Response, -1, 1)) - 3) * Percentage/100)) %>% 
  ggplot(aes(Percentage,Year, fill = Response)) +
  geom_col(position = position_likert()) +
  scale_x_continuous(breaks = seq(-0.5, 0.5, 0.25),
                     labels = percent(abs(seq(-1, 1, 0.5)))) +
  geom_errorbar(aes(y = Year, xmax = mean.resp, xmin = mean.resp), 
                inherit.aes = FALSE) +
  geom_text(aes(label = percent(mean.resp), y = Year, x = mean.resp), 
            hjust = -0.5) +
  scale_fill_brewer(type = "div", palette = "RdYlGn") +
  facet_wrap(~item, dir="v") +
  theme_bw()

创建于2024—04—09,reprex v2.0.2

R相关问答推荐

R gtsummary tBL_summary,包含分层和两个独立分组变量

使用gggplot 2在R中重新调整面板和y轴文本大小

将模拟变量乘以多个观测结果中的模拟变量

R Highcharts与两个位置关联的注释

获取列中值更改的行号

二维样条,严格以一个参数递增

将数字转换为分钟和秒

多个模拟序列间的一种预测回归关系

仅在Facet_WRAP()中的相应方面包含geom_abline()

如何基于两个条件从一列中提取行

派生程序包| ;无法检索';return()';的正文

根据纬度和距离连接两个数据集

在gggraph中显示来自不同数据帧的单个值

在R中使用列表(作为tibble列)进行向量化?

随机将数据帧中特定列上的某些行设置为NA

ggplot斜体轴刻度标签中的单个字符-以前的帖子建议不工作

将边列表转换为路径长度列表

如何编辑被动式数据表?

如何使用list_rind在列表中保留已命名但不包含第0行的记录?

将R中对象的CSV数组转换为JSON数组