I am plotting n plots next to each other using facet_nested_wrap, and i would like that the y-axis por each subfacet is fixed according to the y-axis of the principal facet, but that each principal facet y-axis is independent from each other.
This is the data i am using :
# 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
And I use the following code to make the plots:
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")
)
This results in a plot where the y-axis of every facet is independent from each other. Nevertheles i only want them to be independent within the variable cartera but for each value in cartera i want the y-axis to be fixed. Aditionally, the plot is separating the cartera = "G19" and i dont want thay either. This is how it looks for the moment: What I have
But I want something like this: What I want
I have much more than this three segments of cartera, 8 to be precise, but it can change, i would like some help to solve this problem.
One option would be to create separate plots and combine them using e.g. 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"
)
)
DATA
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)
)