【问题标题】:How to align value labels with geom_text in ggplot如何在ggplot中将值标签与geom_text对齐
【发布时间】:2022-11-25 23:43:40
【问题描述】:

我有这个数据:

structure(list(label2plot = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), levels = c("aaa", "bbb"), class = c("ordered", "factor")), 
    var_abs = c(717L, 569L, 860L, 752L, 713L, 575L, 918L, 724L, 
    946L, 764L, 951L, 764L, 784L, 691L, 672L, 610L, 833L, 671L, 
    773L, 620L, 532L, 293L), var_rel = c(0.557542768273717, 0.442457231726283, 
    0.533498759305211, 0.466501240694789, 0.553571428571429, 
    0.446428571428571, 0.559074299634592, 0.440925700365408, 
    0.553216374269006, 0.446783625730994, 0.554518950437318, 
    0.445481049562682, 0.531525423728814, 0.468474576271186, 
    0.52418096723869, 0.47581903276131, 0.553856382978723, 0.446143617021277, 
    0.554917444364681, 0.445082555635319, 0.644848484848485, 
    0.355151515151515), cohort = structure(c(11L, 11L, 10L, 10L, 
    9L, 9L, 8L, 8L, 7L, 7L, 6L, 6L, 5L, 5L, 4L, 4L, 3L, 3L, 2L, 
    2L, 1L, 1L), levels = c("2010", "2011", "2012", "2013", "2014", 
    "2015", "2016", "2017", "2018", "2019", "2020"), class = "factor")), row.names = c(NA, 
-22L), class = "data.frame")

titel <- "ccc"
subtitel <- " "
werte <- c("aaa", "bbb")
start_abs <- "2010"
ende_abs <- "2020"

我已经按照我的意愿创建了一个情节:

ggplot() +
  geom_bar (
    data = df,
    aes(
      x = factor(label2plot, levels = werte),
      y = var_rel,
      group = cohort,
      fill = forcats::fct_other(cohort, ende_abs, other_level = start_abs)
    ),
    stat = "identity",
    position = position_dodge()
  ) +
  scale_y_continuous(labels = scales::percent, limits = c(0, 1)) +
  theme_classic() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(hjust = 0.5,
                              size = 14,
                              face = "bold"),
    plot.subtitle = element_text(hjust = 0.5),
    plot.caption = element_text(hjust = 0.5),
  ) +
  geom_text(
    size = 3,
    position = position_dodge2(preserve = 'single', width = 0.9),
    hjust = 0.5,
    vjust = -1,
    color = "black",
    aes(
      x = df$label2plot,
      y = df$var_rel,
      label = ifelse(df$cohort == ende_abs, paste0(sprintf(
        "%.1f", df$var_rel * 100
      ), "%"), "")
    )
  ) +
  scale_fill_manual(
    values = c("#AFCDE4", "#389DC3"),
    limits = c(start_abs, ende_abs),
    labels = c(paste0(start_abs, " - ", semvorende_abs), ende_abs)
  ) +
  labs(
    title = str_wrap(titel, 45),
    x = "",
    y =  "",
    fill = "Cohort"
  )

不幸的是,值标签显示在错误的列上方。它们应该显示在深蓝色列上方,而不是最左侧。

我已经尝试了很多东西,但没有用。有人有什么想法吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    问题是分组,即与您的酒吧一样,也在 geom_text 中使用 group=cohort。另外作为一般规则,不要使用df$...。只需将您的数据集移到ggplot 中即可使其成为全局数据集:

    library(ggplot2)
    
    ggplot(data = df) +
      geom_bar (
        aes(
          x = factor(label2plot, levels = werte),
          y = var_rel,
          group = cohort,
          fill = forcats::fct_other(cohort, ende_abs, other_level = start_abs)
        ),
        stat = "identity",
        position = position_dodge()
      ) +
      scale_y_continuous(labels = scales::percent, limits = c(0, 1)) +
      theme_classic() +
      theme(
        legend.position = "bottom",
        plot.title = element_text(hjust = 0.5,
                                  size = 14,
                                  face = "bold"),
        plot.subtitle = element_text(hjust = 0.5),
        plot.caption = element_text(hjust = 0.5),
      ) +
      geom_text(
        size = 3,
        position = position_dodge2(preserve = 'single', width = 0.9),
        hjust = 0.5,
        vjust = -1,
        color = "black",
        aes(
          x = label2plot,
          y = var_rel,
          label = ifelse(cohort == ende_abs, paste0(sprintf(
            "%.1f", var_rel * 100
          ), "%"), ""),
          group = cohort
        )
      ) +
      scale_fill_manual(
        values = c("#AFCDE4", "#389DC3"),
        limits = c(start_abs, ende_abs),
        #labels = c(paste0(start_abs, " - ", semvorende_abs), ende_abs)
      ) +
      labs(
        title = str_wrap(titel, 45),
        x = "",
        y =  "",
        fill = "Cohort"
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多