G_T - 1 year ago 100
R Question

Equal bar widths in ggplot2 histogram using facet_wrap()

I have data which looks similar to example data below and I am attempting to draw a histogram of the measurement column faceted on the Genotype column. Ultimately I would like the colours of the bars to be conditional on the Genotype and Condition columns.

Crucially Genotype B individuals were never measured under condition L.

This is what the data looks like:

``````set.seed(123)
DF <- data.frame(Genotype = rep(c("A", "B"), 500),
Condition = sample(c("E", "L"), 1000, replace = T),
Measurment = round(rnorm(500,10,3), 0))

DF <- anti_join(DF, filter(DF, Genotype == "B" & Condition != "E"))'

Genotype Condition Measurment
1        A         L         18
2        A         L          2
3        B         E         18
4        B         E         18
5        B         E         16
6        B         E         16
``````

Now I to specify the colours of the bars I thought it easiest to create a new column of hexcodes such that all individuals of Genotype B are one colour, and individuals of Genotype A are a second colour if measured under Condition E and a third colour if measured under Condition L.

``````DF <- DF %>% mutate(colr = ifelse(Genotype == "B", "#409ccd",
ifelse(Condition == "E", "#43cd80", "#ffc0cb")))
``````

I can then draw a histogram faceted on the Genotype column like so:

``````ggplot(data=DF, aes(Measurment, fill = Condition)) +
geom_histogram(aes(y=..count.., fill = colr), position='dodge', binwidth = 1) +
facet_wrap(~Genotype, nrow=2) +
scale_fill_manual(values = c("#409ccd","#ffc0cb","#43cd80")) +
theme(legend.position="none")
``````

and it like like this:

However as you can see the columns for Genotype B are twice the size of Genotype A. How can I shrink the Genotype B to the same size as Genotype A?

I considered adding dummy entries to my data where Genotype B has Condition L entries but the binning function then counts these as Measurements which is misleading. I also have a version of this using
`geom_bar()`
but that results in a similar problem. ggplot must have a way of doing this.

Any help appreciated.

``````ggplot(data=DF, aes(Measurment, fill = Condition)) +