I am working in the updated ggplot2 (2.2.0). A great new feature is the ability to plot along each x-axis for an uneven number of plots, as per this page.
I wish to plot with the same x-axis in each facet. However, this does not work when missing data is introduced. As an example, I shall draw upon the example data in this question.
# Example dataset
dataf <- data.frame(x=c(1:30), A=rnorm(30,20,5), B=rnorm(30,15,0.5))
datam <- melt(dataf, id="x")
# Plot in ggplot2
ggplot(datam, aes(factor(x), value)) +
geom_bar(stat="identity") +
facet_wrap(~variable,nrow = 2,scales = "free")
When I remove particular rows and introduce missing data (as per my code below), I am no longer to have the same x-axis labels for both "A" and "B".
# Remove certain rows to introduce missing data
datam <- datam[-c(2, 4, 58), ]
I can plot missing values and align to the bottom x-axis using scales = fixed
although I no longer have x-axis tick mark labels for group "A" in the facet.
ggplot(datam, aes(factor(x), value)) +
geom_bar(stat="identity") +
facet_wrap(~variable,nrow = 2,scales = "fixed")
How do have the exact same x-axis labels showing for every facet, even when there is missing data?
Thank you.