Grouped Boxplot: Keep subgroups together

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):

My guess is something like this can be piped to ggplot() to get what you want:

data |> 
  arrange(condition, sample) |> 
  mutate(sample = fct_inorder(sample))

(The function fct_inorder() uses the current table order to order the resulting factor, I'm pretty sure.)

1 Like

to remove the gaps, try
facet_wrap(~condition, scales = "free_x")

1 Like

That works as a back up, but I'd prefer not to facet. Thank you.

That's perfect. Thank you!

data <- data %>% arrange(condition)
ggplot(data, aes(x = forcats::fct_inorder(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',
  )

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.