I'm learning how to use geom_line with facet_nested and I cannot produce a continuous line across facets and between each pair of facets there is a gap. What I try to do is to obtain a clean and continuous line as if I produced the very same graphic using Microsoft Excel. Before asking this question here, I searched and I found the following thread in StackOverflow :
How can I draw geom_line across facets or grid
Despite the appearance, this is not exactly what I'm looking for, given that the author doesn't really group the data as facet_nested does. So in order to better see what is my problem, I provide a test case below that you can easily copy/paste in RStudio (Windows environment) in order to understand where is the problem.
Here is the dataframe that I use for my graphic:
df_graph_data = data.frame(
year = c(
rep.int("2020", times = 11),
rep.int("2021", times = 12),
rep.int("2022", times = 3)
),
month_name = c(
"Feburary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December",
"January", "Feburary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December",
"January", "Feburary", "March"
),
month_number = c(
"02", "03", "04", "05", "06", "07",
"08", "09", "10", "11", "12", "01",
"02", "03", "04", "05", "06", "07",
"08", "09", "10", "11", "12", "01",
"02", "03"
),
number_of_queries = c(
484819L, 576697L, 843015L, 925175L,
1102853L, 889212L, 835706L, 774622L,
701338L, 850297L, 1046064L, 1273363L,
958868L, 1088284L, 1151606L, 1666950L,
2025731L, 2731704L, 2429019L, 3228395L,
3204915L, 2612807L, 2811946L, 3053788L,
2589273L, 2305433L
)
)
###
###
### I add the following variable in order to be able to identify and reference
### each observation in my data.frame uniquely
df_graph_data$rownum = 1:nrow(df_graph_data)
And the graphic that I produced using tha above dataframe
library("tidyverse")
library("ggh4x")
options(device = "windows")
###
###
require(scales)
ggplot(data = df_graph_data) +
geom_line(mapping = aes(
x = rownum,
y = number_of_queries
),
size = 1,
colour = "blue",
linetype = "solid"
) +
scale_x_continuous(
breaks = seq(
min(df_graph_data$rownum),
max(df_graph_data$rownum),
by = 1L
),
labels = df_graph_data$month_number
) +
scale_y_continuous(
limits = c(0, max(df_graph_data$number_of_queries) + 1000000L),
breaks = seq(0, max(df_graph_data$number_of_queries) + 1000000L,
by = 250000L
),
labels = comma,
expand = expansion(mult = 0, add = 0)
) +
geom_point(mapping = aes(
x = rownum,
y = number_of_queries),
shape = 15
) +
facet_nested(
~ year,
switch = "x",
scales = "free_x",
space = "free_x"
) +
labs(
x = "Month",
y = "Number of clients queries",
caption = "Statistics on queries"
) +
theme(
strip.placement = "outside",
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=0.5),
axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0)),
axis.title.x = element_text(margin = margin(t = 15, r = 0, b = 0, l = 0)),
plot.title = element_text(hjust = 0.5, size = 12),
plot.caption = element_text(size = 8, hjust = 1),
plot.caption.position = "plot",
panel.spacing.x=unit(0.0, "lines"),
panel.spacing.y=unit(0.0, "lines")
)
I join here the following screenshot that details more my problem
Is there any solution available in ggplot2 for the two problems indicated in the above screenshot in particular for having a continuous line across facets?