为什么scale_x_discrete会回收数据?

oxcyiej7  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(151)

根据该数据:

library(tidyverse)
library(ggplot2)
library(ggtext)

df <- structure(
  list(
    variable = c(
      "var1",
      "var1",
      "var1",
      "var2",
      "var2",
      "var2",
      "var3",
      "var3",
      "var3",
      "var4",
      "var4",
      "var4"
    ),
    gruppe = structure(
      c(1L, 7L, 16L,
        1L, 7L, 16L, 1L, 7L, 16L, 1L, 7L, 16L),
      levels = c(
        "AAAA",
        "BBBB",
        "CCCC",
        "DDDD",
        "EEEE",
        "FFFF",
        "GGGG",
        "HHHH",
        "IIII",
        "JJJJ",
        "KKKK",
        "LLLL",
        "MMMM",
        "NNNN",
        "OOOO",
        "PPPP GGGG"
      ),
      class = "factor"
    ),
    auspraegung = c(
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum",
      "lorem ipsum"
    ),
    n = c(148L, 32L, 89L, 135L, 31L, 93L,
          123L, 28L, 76L, 115L, 26L, 67L),
    prozent = c(88.1, 84.2,
                85, 81.3, 81.6, 89, 74.1, 73.7, 72, 69.7, 68.4, 65),
    gruppen_variablen_summe = c(168L,
                                38L, 105L, 166L, 38L, 105L, 166L, 38L, 105L, 165L, 38L, 103L),
    auspr_n = c(
      "88.1% \nn = 148",
      "84.2% \nn = 32",
      "85% \nn = 89",
      "81.3% \nn = 135",
      "81.6% \nn = 31",
      "89% \nn = 93",
      "74.1% \nn = 123",
      "73.7% \nn = 28",
      "72% \nn = 76",
      "69.7% \nn = 115",
      "68.4% \nn = 26",
      "65% \nn = 67"
    ),
    gruppe_n = c(
      "AAAA \nn = 168",
      "GGGG \nn = 38",
      "PPPP GGGG \nn = 105",
      "AAAA \nn = 166",
      "GGGG \nn = 38",
      "PPPP GGGG \nn = 105",
      "AAAA \nn = 166",
      "GGGG \nn = 38",
      "PPPP GGGG \nn = 105",
      "AAAA \nn = 165",
      "GGGG \nn = 38",
      "PPPP GGGG \nn = 103"
    )
  ),
  class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
  row.names = c(NA,-12L),
  groups = structure(
    list(
      variable = c(
        "var1",
        "var1",
        "var1",
        "var2",
        "var2",
        "var2",
        "var3",
        "var3",
        "var3",
        "var4",
        "var4",
        "var4"
      ),
      gruppe = structure(
        c(1L,
          7L, 16L, 1L, 7L, 16L, 1L, 7L, 16L, 1L, 7L, 16L),
        levels = c(
          "AAAA",
          "BBBB",
          "CCCC",
          "DDDD",
          "EEEE",
          "FFFF",
          "GGGG",
          "HHHH",
          "IIII",
          "JJJJ",
          "KKKK",
          "LLLL",
          "MMMM",
          "NNNN",
          "OOOO",
          "PPPP GGGG"
        ),
        class = "factor"
      ),
      .rows = structure(
        list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
             10L, 11L, 12L),
        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
  )
)

varlabels <- c(
  "var1" = "Text var 1",
  "var2" = "Text var 2",
  "var3" = "Text var 3",
  "var4" = "Text var 4"
)

因为我想让图中的条形图按照一定的顺序排列,所以我指定了一个id:

df$gruppen_id <- rep(c(2, 1, 3), times = 4)

现在我创建了一个图,这也是可以的。不幸的是,n (total)的值在轴标签中没有正确显示。这里的值似乎是循环使用的。

df %>%
  ggplot(aes(
    x = fct_reorder(str_wrap(gruppe, width = 30),-gruppen_id),
    y = prozent,
    fill = gruppe
  )) +
  geom_bar(
    na.rm = T,
    stat = "identity",
    position = position_dodge2(preserve = "total"),
    show.legend = F
  ) +
  geom_text(
    aes(label = paste(sprintf("%1.1f%%", prozent), "\nn =", n)),
    size = 3,
    hjust = 1.3,
    vjust = 0.5,
    position = position_dodge2(.9),
    colour = "white"
  ) +
  scale_fill_manual(values = c("#6DBE99", "#004B93", "#389DC3")) +
  labs(title = "Text title\n") +
  labs(caption = "Text caption") +
  coord_flip() +
  facet_wrap(
    ~ variable,
    nrow = 4,
    as.table = T,
    labeller = as_labeller(varlabels)
  ) +
  xlab("") +
  ylab("") +
  guides(fill = guide_legend(title = "")) +
  expand_limits(x = 2, y = 100) +
  scale_x_discrete(label = paste(
    str_wrap(df$gruppe, 30),
    "\nn (total) =",
    df$gruppen_variablen_summe
  )) +
  scale_y_continuous(
    expand = c(NA, 100),
    breaks = c(0, 25, 50, 75, 100),
    labels = c("0%", "25%", "50%", "75%", "100%\n")
  ) +
  theme(
    panel.background = element_rect(fill = "white", colour = NA),
    panel.grid.major.x = element_line(color = "#EAEAEA"),
    axis.title.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_blank(),
    strip.text.x = element_text(
      lineheight = 1,
      size = 10,
      angle = 0,
      hjust = 0
    ),
    strip.background = element_rect(color = NA, fill = NA),
    plot.background = element_rect(fill = "white", color = "white"),
    panel.spacing.y = unit(2, "lines"),
    plot.title = element_text(
      size = 11,
      lineheight = 1,
      face = "bold",
      hjust = -0.5,
      vjust = -2
    ),
    plot.margin = margin(0.3, 0.5, 0.5, 0.3, "cm"),
    plot.caption.position = "plot",
    plot.caption = element_text(
      hjust = 0,
      vjust = 0.5,
      margin = margin(0, 0, 0, 0.1, "cm")
    )
  )

sbdsn5lh

sbdsn5lh1#

恕我直言,将 Dataframe 中的向量直接分配给刻度的labels或...参数总是容易出错,在大多数情况下,有更简单的方法来实现所需的结果,即在您的情况下,向包含x轴标签的数据中添加一列。然后将此列Map到x上,并在facet_wrap中设置scales="free_y"
注意:我已经将您的代码精简为一个更小的示例。

library(ggplot2)

df$x <- paste(str_wrap(df$gruppe, 30), "\nn (total) =", df$gruppen_variablen_summe)

ggplot(df, aes(
    x = fct_reorder(x, -gruppen_id),
    y = prozent,
    fill = gruppe
  )) +
  geom_bar(
    na.rm = T,
    stat = "identity",
    position = position_dodge2(preserve = "total"),
    show.legend = F
  ) +
  scale_fill_manual(values = c("#6DBE99", "#004B93", "#389DC3")) +
  labs(title = "Text title\n", x = NULL, y = NULL) +
  labs(caption = "Text caption") +
  coord_flip() +
  facet_wrap(
    ~ variable,
    nrow = 4,
    as.table = T,
    labeller = as_labeller(varlabels),
    scales = "free_y"
  ) +
  guides(fill = guide_legend(title = "")) +
  expand_limits(x = 2, y = 100) +
  scale_y_continuous(
    expand = c(NA, 100),
    breaks = c(0, 25, 50, 75, 100),
    labels = c("0%", "25%", "50%", "75%", "100%\n")
  )

相关问题