在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?我该如何做到这一点?
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))