如何在R中插入复杂的水平条标签?

ctehm74n  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(88)

我有以下样本数据集。基础是在两个时间点进行的调查。调查结果分为两组(group1和group2):

df <- structure(list(label2plot = c("yes", "no", "yes", "no",
"yes", "no", "yes", "no"), gruppe = c("group1", "group1",
"group2","group2", "group1", "group1", "group2", "group2"), 
cohort = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), levels = c("20172018", "20192020"), class = "factor"), var_abs = c(4979L,
 3214L, 129L, 130L, 5476L, 3556L, 89L, 74L), var_rel = c(0.789565,
 0.8035, 0.871622, 0.8125, 0.793163, 0.805254, 0.839623,
 0.860465)), class = "data.frame", row.names = c(NA, -8L))

字符串
在此基础上,我创建了条形图。对于每一组,我都有一个单独的图表并排放置。

var_label <- "blabla"
title <- "blublub"
subtitle <- "blablub"
val_label <- c("yes", "no")

packages2check <- c("ggplot2", "stringr")

for (package in packages2check) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package)
    library(package, character.only = TRUE)
  } else {
    library(package, character.only = TRUE)
  }
}

ggplot() +
  geom_bar (
    data = df,
    aes(
      x = factor(label2plot, levels = val_label),
      y = var_rel,
      group = cohort,
      fill = label2plot
    ),
    stat = "identity",
    position = position_dodge(),
    color = "white",
    linewidth = 0.5
  ) +
  scale_y_continuous(
    labels = scales::percent,
    limits = c(0, 1.15),
    breaks = seq(0, 1, 0.2)
  ) +
  theme_classic() +
  theme(
    legend.position = "none",
    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),
  ) +
  scale_fill_manual(values = c("yes" = "#AFCDE4", "no" = "#389DC3")) +
  geom_text(
    data = df,
    size = 3,
    position = position_dodge2(preserve = 'single', width = 0.9),
    hjust = 0.5,
    vjust = -0.5,
    color = "black",
    aes(
      x = factor(label2plot, levels = val_label),
      y = var_rel,
      label = paste0(sprintf("%.1f", var_rel * 100), "%", "\n(N=", var_abs, ")")
    )
  ) +
  labs(
    subtitle = paste(subtitle),
    title = str_wrap(title, 45),
    x = var_label,
    y =  "",
    fill = ""
  ) +
  facet_wrap( ~ gruppe)


在单独的图表中,显示了两个时间点的“是”和“否”结果。将来会有更多的时间点。但是,由于现在只有两个时间点,因此“是”和“否”结果用两个相同颜色的条形表示。
我可以根据需要将时间点(或队列)的标签垂直插入到条形图中。但是,标签仅显示在每个条形图的左侧,而不是右侧。
它应该看起来像示例图表。我怎么做?

4ioopgfo

4ioopgfo1#

底线:您需要position=与前面的相同,并且可以使用y = min(var_rel)/2放置它们。
我将通过将factor(.)置于ggplot2之外来简化一点,在主ggplot(.)调用中定义x=y=美学,所有几何体都将受益于简化。这样做的一个好处是,通过使用factor,您将对勒贝尔施加特定的 * 顺序 *。然而,通过混合使用因子和非因子,(即fill=label2plot),那么它们在图的所有部分中的顺序可能不一致(例如,图例)。也许这不适用于这里(还?),但这通常是一个很好的实践。

gg <- df |>
  transform(label2plot = factor(label2plot, levels = val_label)) |>
  ggplot(aes(x = label2plot, y = var_rel)) +
  geom_bar (
    aes(
      group = cohort,
      fill = label2plot
    ),
    stat = "identity",
    position = position_dodge(),
    color = "white",
    linewidth = 0.5
  ) +
  scale_y_continuous(
    labels = scales::percent,
    limits = c(0, 1.15),
    breaks = seq(0, 1, 0.2)
  ) +
  theme_classic() +
  theme(
    legend.position = "none",
    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),
  ) +
  scale_fill_manual(values = c("yes" = "#AFCDE4", "no" = "#389DC3")) +
  geom_text(
    size = 3,
    position = position_dodge2(preserve = 'single', width = 0.9),
    hjust = 0.5,
    vjust = -0.5,
    color = "black",
    aes(
      label = paste0(sprintf("%.1f", var_rel * 100), "%", "\n(N=", var_abs, ")")
    )
  ) +
  labs(
    subtitle = paste(subtitle),
    title = str_wrap(title, 45),
    x = var_label,
    y =  "",
    fill = ""
  ) +
  facet_wrap( ~ gruppe)

字符串
从这里,我们可以简单地做:

gg +
  geom_text(
    aes(y = min(var_rel)/2, label = cohort),
    angle = 90, position = position_dodge2(preserve = 'single', width = 0.9)
  )


的数据

fslejnso

fslejnso2#

您可以沿以下行沿着向图中添加项.

+ geom_text(
    data = df,
    size = 4,            #larger text
    position = position_dodge2(preserve = 'single', width = 0.9),
    hjust = 0.5,
    vjust = 0.5,         #to centre in bar
    angle = 90,          #write vertically
    color = "black",
    aes(
      x = factor(label2plot, levels = val_label),
      y = 0.4,           #may need to adjust position
      label = cohort)    #label required
  )

字符串

相关问题