A previous question describes the problem of that the location for missing bars is not preserved in ggplot2. The accepted solution was then to add zeros to complete the missing values. However, in some cases this could be not desirable because zero height bars can be interpreted as bars where the value is zero, instead of missing values. Next I present an example to better describe the question.
Consider the following R code:
library(ggplot2)
data = data.frame(
x = c("s", "s", "s", "r", "r", "r", "s", "s", "r", "r"),
y = c(1, 2, 2, 3, 2, 1, 3, 1, 2, 1),
z = c("A", "B", "C", "A", "B", "C", "A", "C", "A", "C"),
group = c("group 1", "group 1", "group 1", "group 1", "group 1", "group 1",
"group 2", "group 2", "group 2", "group 2")
)
ggplot(data, aes(x=x, y=y, fill=z)) +
facet_wrap(~group, ncol = 3) +
stat_summary(fun=mean, geom="bar", position=position_dodge(preserve = "single"), color="grey70")
This code generates the following plot:
Observe that the green bar for variable z = B does not appear in group 2 because there is no data for variable z = B in group 2. I would like to have a space between the red and blue bars in group 2 to make emphasis on the missed data. Now the space is after the blue bar.
We can set the missing values with 0 to separate bars A and B. That is:
data <- rbind(data, list("s", 0, "B", "group 2"))
data <- rbind(data, list("r", 0, "B", "group 2"))
The problem is that (as is shown below) this solution draws bars with height zero. This is confusing because one may think that there is some data with value zero.
I would prefer having no bars and preserving the space. Moreover, I would like to write a vertical text "no data" over the missing bar. Is this possible?