How to bring box plots in the right order without changing colors or variables?

Thank you for your patience! Now the colors are off and the order.

disg <-dis %>% 
  mutate(group = forcats::fct_relevel(group, "Control", "Coccidia", "S. Typhimurium")) %>%
  ggplot(aes(x=group, y=distances, fill=group)) + 
  geom_boxplot() +
  scale_fill_manual(values = c("#00BFC4", "#F8766D","#C77CFF","#7CAE00")) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
  labs(x = "", y = "Distance to centroid", fill="Treatment")

**Error message:
1: Problem with mutate() input group.
:information_source: Unknown levels in f: Coccidia
:information_source: Input group is forcats::fct_relevel(group, "Control", "Coccidia", "S. Typhimurium").
2: Unknown levels in f: Coccidia **

Ah, you don't have 'Coccidia' in your data, you have 'Coccidia ' - note the space.
You should be able to fix it with

df %>% 
  mutate( group = forcats::fct_relevel(trimws(group), "Control", "Coccidia", "S. Typhimurium"))

Thank you so so so much for your help!
It was a long and exhausting journey, but I learned a lot (my first reprex!) and at the end I could even find the space you mentioned.
Wow. I am very very grateful that you spent all the time to guide me through.
Now, look at that:

Here I managed to sort the box-plots in the right order.
However, it is not obvious to me, where I shall change the colors.
scale_colour_manual(Treatment=c("#00BFC4", "#F8766D", "#7CAE00", "#C77CFF")) + didn't work and scale_fill_manual is not supported with gather.

adiv16 <- data.frame(
  "Observed" = phyloseq::estimate_richness(gps16, measures = "Observed"),
  "Shannon" = phyloseq::estimate_richness(gps16, measures = "Shannon"),
  "PD" = picante::pd(samp = data.frame(t(data.frame(phyloseq::otu_table(gps16)))), tree = phyloseq::phy_tree(gps16)) [ ,1],
  "Treatment" = phyloseq::sample_data(gps16)$treatment)
adiv_plot16 <- adiv16 %>%
  mutate(Treatment = forcats::fct_relevel(Treatment, "Control", "Coccidia", "S. Typhimurium")) %>%
  gather(key = metric, value = value, c("Observed", "Shannon", "PD")) %>%
  mutate(metric = factor(metric, levels = c("Observed", "Shannon", "PD"))) %>%
  ggplot(aes(group=Treatment, x = Treatment, y = value)) +
  geom_boxplot(outlier.color = NA) +
  geom_jitter(aes(color = Treatment), height = 0, width = .2) +
  labs(x = "", y = "") +
  facet_wrap(~ metric, scales = "free") +
  theme(legend.position="bottom") +
  theme(axis.text.x = element_blank(), axis.ticks.x=element_blank())


Hi Lacona,

Well done on your first reprex! It was perfect for that question. But suppose the space was on 'S. Typhimutium', then I would not have been able to spot the issue, and would have had to ask for another reprex!

And, unfortunately, that's the issue with your new query. Again, I don't have adiv16 or gps16 so I can't tell what the data actually looks like I'm afraid.

And I think that it is probably important here because as far as I know there should be no issue with scale_fill_manual and gather. I suspect adiv16 is a somewhat nested dataframe? So to use ggplot2 you would have to flatten it out somehow - see the unnest functions in the tidyr package.
You should either supply a new dataset that has the same structure with fake data, or else use dput again. I reckon I could probably find the issue then.

However, even better in this instance, since I know nothing of bioconductor, would be to create your reprex and post it as a separate question, where someone who does know bioconductor could help.


Solution was to generate a color object first:
scale_col<-scale_color_manual(values=c('Control'="#00BFC4", 'Coccidia'="#F8766D",'S. Typhimurium'="#C77CFF",'Coccidia and S. T.'="#7CAE00"))
and then add scale_col +into my code.

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.