Hi all,
I have created a boxplot with groups and subgroups. However I want to keep the same colours (subgroups) together. So all greens on one side. All reds on the other side.
I think it has to do with reordering the levels of the x-axis data. However I don't know how to reorder the levels of a categorical variable with another categorical variable.
Thanks in advance,
Kenneth
Data:
> head(data)
# A tibble: 6 × 5
gene sample count condition trial
<chr> <fct> <int> <fct> <fct>
1 ENSSSCG00000028996 29 -18758 High 2
2 ENSSSCG00000028996 31 7157 High 2
3 ENSSSCG00000028996 34 4819 High 2
4 ENSSSCG00000028996 35 -18888 High 2
5 ENSSSCG00000028996 45 -24429 High 2
6 ENSSSCG00000028996 79 -10963 High 2
Boxplot:
ggplot(data, aes(x = sample, y = count, fill = condition)) +
geom_boxplot(coef = 2) +
ylim(c(-4,4)) +
labs(title = "Raw") +
ylab('Median deviation of log expression') +
xlab('Sample') +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(),
legend.position = 'bottom',
) #+ facet_wrap(~condition)
Without facet wrap (I want this, but with same colors grouped together):
With facet wrap (but this adds gaps, looks clunky, and takes up too much space):