Every time I make a fasted bar chart, if there is a section with no data ggplot will still make a spot for it in its aim for symmetry and thus it looks like these variables were tested and received a value of 0 which is not the case. Is there a way to override ggplot. Below is an example where the section 8 A3 looks like it record a 0 value but actually it was not investigated in the data set.
library(ggplot2)
library(dplyr)
# Your data
data <- data.frame(A = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
B = c(5, 34, 5, 34, 34, 56, 3, 3, 6, 35),
C = c("Light","Light", "Light", "Dark", "Dark", "Dark", "Light","Light", "Dark", "Dark"),
D = c(4, 4, 4, 4, 4, 4, 8, 8, 8, 8))
# Create plots
Plot1 <- ggplot(data, aes(x = A, y = B, fill = C)) +
geom_bar(stat = "identity", position = position_dodge2(preserve = "single")) +
theme_minimal() + theme(legend.position = "right") +
facet_wrap(~D)
Plot1


scales = "free_x" drops the unused x value
If you want to keep all the bars the same size use facet_grid with the space argument.
Solution provided below in ggplot2 - and also ggblanket
Created on 2024-02-24 with reprex v2.1.0