Issue:
I'm dealing with groups of graphics that sometimes share ranges (not displayed in the reprex data), which I want to display side by side, ggh4x::facet_manual
is the perfect function for the job. However I can't find a way for it to display the x-axis when a panel area is left blank under a graph.
Reproductible exemple and expected output:
facet_manual()
documentation doesn't offer this option from the get-go.
axes
A character(1) or logical(1) where axes should be drawn. One of the following:
"margins" or FALSE Only draw axes at the outer margins (default).
"x" Draw axes at the outer margins and all inner x-axes too.
"y" Draw axes at the outer margins and all inner y-axes too.
"all" or TRUE Draw the axes for every panel.
Reprex:
# Data ----
tibble::tibble(
date = as.Date(rep(
rep(
c(
"2022-01-15", "2022-02-15", "2022-03-15", "2022-04-15", "2022-05-15",
"2022-06-15", "2022-07-15", "2022-08-15", "2022-09-15", "2022-10-15",
"2022-11-15", "2022-12-15", "2023-01-15", "2023-02-15", "2023-03-15",
"2023-04-15", "2023-05-15", "2023-06-15", "2023-07-15", "2023-08-15",
"2023-09-15", "2023-10-15", "2023-11-15", "2023-12-15"
),
2
),
rep(c(1L, 5L), each = 24L)
)),
group = c(
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B", "C", "E", "F", "D", "B", "C",
"E", "F", "D", "B", "C", "E", "F", "D", "B"
),
value = c(
1032.8, 976.4, 885.6, 893.7, 793.8, 777.4, 847.8, 815.9, 795.4, 862.7, 873.4,
1086.4, 964.1, 870.4, 837.8, 812, 778, 759.1, 731.4, 759.6, 778.3, 820.3,
857.7, 943.7, 200.4, 38.6, 20.6, 64.5, 245.7, 188.2, 38.6, 17.6, 56.2, 235.1,
182.6, 36.7, 17.4, 56.6, 238.6, 183.9, 34.6, 17.6, 65.5, 238.6, 165.5, 34.1,
16.5, 48.8, 234, 157.8, 34.3, 16.1, 47.8, 237.8, 164.2, 34.3, 16.9, 52, 241.1,
157.8, 34.8, 17, 51.8, 241.6, 160, 36.4, 16.1, 47.4, 242.3, 173.9, 36.6, 17.1,
55.3, 246.9, 179.7, 38.1, 17.4, 62.3, 242.8, 219.9, 42.5, 21.1, 103.7, 257.5,
201.9, 40.6, 18.3, 93.6, 240.3, 186.6, 37.5, 18.9, 72.7, 238.7, 182.4, 36.7,
18.6, 63.4, 237, 173.4, 34.3, 17.8, 56.3, 231.5, 164.1, 34, 16, 54, 227.9,
150.1, 31.9, 15, 48.8, 235.6, 145.6, 31.7, 14, 46.8, 234.6, 151.9, 32.3, 16,
48.8, 234.7, 154.3, 33.8, 15.8, 49, 239.7, 163.8, 36.8, 16.9, 56, 243.4,
177.4, 37.3, 18, 60.2, 243.9, 196, 37.4, 20.2, 76.8, 243.5
),
)
# Code ----
# install.packages("ggh4x")
library(ggplot2)
library(ggh4x)
design <- c("a#
bc
d#
ef
#h")
ggplot(data = reprex_data) +
geom_point(aes(x = date, y= value),
colour = "black",size = 1.5, shape = 19, alpha = 0.2) +
geom_line(aes(x = date, y = value),
colour = "black",linewidth = 0.5, alpha = 0.2) +
facet_manual(~ group, design, scales = "free_y")
One option would be to use ggh4x::facetted_pos_scales
to add an axis, i.e. free the x scale and get rid of the undesired axes. However, a downside is that it will add some empty space in the first column, too.
library(ggplot2)
library(ggh4x)
design <- c("a#
bc
d#
ef
#h")
ggplot(data = reprex_data) +
geom_point(aes(x = date, y = value),
colour = "black", size = 1.5, shape = 19, alpha = 0.2
) +
geom_line(aes(x = date, y = value),
colour = "black", linewidth = 0.5, alpha = 0.2
) +
facet_manual(~group, design, scales = "free") +
facetted_pos_scales(
x = list(
!group %in% c("C", "E", "F") ~ scale_x_date(breaks = NULL)
)
)