在垂直轴的多个 ggplot2 图中包含平均值

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

我在 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))
r dataframe ggplot2
1个回答
1
投票

假设我们可以平均李克特量表反应(somewouldadvise这是没有意义的,因为这些反应可能有非线性解释),你可以这样做:

data_f %>%
  ggplot(aes(Percentage,Year)) +
  geom_col(aes(fill = Response), position = ggstats::position_likert()) +
  geom_linerange(aes(ymin = as.numeric(Year) - 0.4,  ymax = as.numeric(Year) + 0.4),
                     data = data_f %>%
               summarize(Percentage = sum((parse_number(Response)-3) * Percentage / 100))) + 
  ...

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