I'm using ggplot (v1.0.1) to plot histograms with bar fill mapped to a binary State
factor and using position = "dodge"
. The histograms are facetted by a second Group
factor. When one level of the State
factor is absent from a group, the plot for that facet differs from those where both levels are present. The following toy example illustrates this:
set.seed(1234)
states <- c("Alive", "Dead")
dat <- data.frame(Group = rep(LETTERS[1:2], each=100),
Measure = rnorm(200, 50, 10),
State = c(rep(states, 50), rep("Alive", 100)))
ggplot() +
geom_histogram(data=dat, aes(x=Measure, fill=State),
position="dodge", binwidth=5) +
facet_wrap(~ Group)
What I would like is for the bars in the facet for Group B to have the same width and relative position as those for Group A. I thought facet_wrap(~ Group, drop = FALSE)
would accomplish this but it doesn't seem to have any effect. The following work-around achieves the desired plot:
dat <- rbind(dat, data.frame(Group="B", Measure=50, State=NA))
ggplot() +
geom_histogram(data=dat, aes(x=Measure, fill=State),
position="dodge", binwidth=5) +
scale_fill_discrete(na.value = NA) +
facet_wrap(~ Group)
But I'm sure I must be missing a ggplot option that would accomplish this without having to hack the data.
ggplot
is generally very good at plotting the data in your data frame, I'm not sure that I'd consider making your data frame better match what you want to plot "hacking the data".tidyr
has thecomplete()
function for just such a transformation. – Gregor Thomasposition_dodge
, I'd usealpha
transparency or another faceting dimension. – Roland