每个面的 y 轴独立值,但每个子面 (R) 是固定的

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

我正在使用facet_nested_wrap绘制彼此相邻的n图,并且我希望每个子面的y轴根据主面的y轴固定,但每个主面y轴是独立的来自彼此。

这是我正在使用的数据:

# A tibble: 94 x 6
   cia   fec_cte cartera tipo_cf tipo_importe valor_importe
   <chr> <chr>   <chr>   <chr>   <chr>                <dbl>
 1 0185  202312  G14     LIC     ARNF                  45.1
 2 0185  202312  G14     LIC     Descontado          2847. 
 3 0185  202312  G16     LIC     ARNF                  49.6
 4 0185  202312  G16     LIC     Descontado         33068. 
 5 0185  202312  G17     LIC     ARNF               10030. 
 6 0185  202312  G17     LIC     Descontado        183566. 
 7 0185  202312  G19     LIC     ARNF                1523. 
 8 0185  202312  G19     LIC     Descontado         10962. 
 9 0185  202312  G20     LIC     ARNF               -1030. 
10 0185  202312  G20     LIC     Descontado         86038. 
# i 84 more rows

我使用以下代码来绘制图:

library(ggh4x)
library(plotly)
library(tidyverse)

data %>% 
  filter(cia == "0185") %>% 
  ggplot(
    aes(x = fec_cte, y = valor_importe, fill = fec_cte)
  ) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(title = "BEL 0185") +
  facet_nested_wrap(. ~ cartera + tipo_importe , scales = "free") +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  theme(
    plot.title = element_text(hjust = 0.5,vjust = 2, face = "bold"),
    plot.margin = margin(t = 10, r = 20, b = 10, l = 20),
    axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0),face = "bold"),
    axis.title.x = element_text(face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    strip.background = element_rect(fill="red"),
    strip.text = element_text(colour = 'white', face = "bold")
  )

这会产生一个图,其中每个面的 y 轴彼此独立。尽管如此,我只希望它们在变量 cartera 中独立,但对于 cartera 中的每个值,我希望 y 轴是固定的。另外,情节将 cartera =“G19” 分开,我也不想要。 目前看起来是这样的: 我有什么

但我想要这样的东西: 我想要什么

我的卡特拉的数量远不止这三段,准确地说是8段,但它可以改变,我需要一些帮助来解决这个问题。

r ggplot2 visualization facet facet-wrap
1个回答
0
投票

一种选择是创建单独的图并使用例如将它们组合起来

patchwork

library(ggh4x)
library(tidyverse)
library(patchwork)

plot_fun <- function(.data) {
  ggplot(
    .data,
    aes(x = fec_cte, y = valor_importe, fill = fec_cte)
  ) +
    geom_col(
      position = "dodge", color = "black"
    ) +
    facet_nested_wrap(. ~ cartera + tipo_importe) +
    scale_y_continuous(labels = scales::comma)
}

data |>
  filter(cia == "0185") |>
  split(~cartera) |>
  lapply(plot_fun) |>
  wrap_plots(
    guides = "collect",
    axes = "collect_y",
    axis_titles = "collect",
    ncol = 2
  ) +
  plot_annotation(
    title = "BEL 0185"
  ) &
  theme_bw() &
  theme(
    plot.title = element_text(
      hjust = 0.5, vjust = 2, face = "bold"
    ),
    # plot.margin = margin(t = 10, r = 20, b = 10, l = 20),
    axis.title.y = element_text(
      margin = margin(t = 0, r = 10, b = 0, l = 0),
      face = "bold"
    ),
    axis.title.x = element_text(face = "bold"),
    # axis.text.x = element_text(angle = 45, hjust = 1),
    strip.background = element_rect(fill = "red"),
    strip.text = element_text(
      colour = "white",
      face = "bold"
    )
  )

enter image description here

数据

data <- data.frame(
  cia = c("0185", "0185", "0185", "0185", "0185", "0185", "0185", "0185", "0185", "0185"),
  fec_cte = c("202312", "202312", "202312", "202312", "202312", "202312", "202312", "202312", "202312", "202312"),
  cartera = c("G14", "G14", "G16", "G16", "G17", "G17", "G19", "G19", "G20", "G20"),
  tipo_cf = c("LIC", "LIC", "LIC", "LIC", "LIC", "LIC", "LIC", "LIC", "LIC", "LIC"),
  tipo_importe = c("ARNF", "Descontado", "ARNF", "Descontado", "ARNF", "Descontado", "ARNF", "Descontado", "ARNF", "Descontado"),
  valor_importe = c(45.1, 2847, 49.6, 33068, 10030, 183566, 1523, 10962, -1030, 86038)
)
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.