fct_reorder issues

Hi experts,

I am having trouble using fct_reorder command.

library(tidyverse)
library(readxl)
library(ggplot2)
library(reprex)

families <- tibble::tribble(
  ~class,   ~phylum,                                        ~family,                                      ~genus, ~mean,  ~sd,
   "OTP", "ko00780",                            "Biotin metabolism",      "Metabolism of cofactors and vitamins",  1.92, 0.04,
   "OTP", "ko00860",                         "Porphyrin metabolism",      "Metabolism of cofactors and vitamins",  0.68, 0.01,
   "OTP", "ko00680",                           "Methane metabolism",                         "Energy metabolism",  0.47,    0,
   "OTP", "ko00920",                            "Sulfur metabolism",                         "Energy metabolism",  0.18, 0.05,
   "OTP", "ko00910",                          "Nitrogen metabolism",                         "Energy metabolism",   0.7, 0.01,
   "OTP", "ko00650",                         "Butanoate metabolism",                   "Carbohydrate metabolism",  0.71, 0.01,
   "OTP", "ko00630",      "Glyoxylate and dicarboxylate metabolism",                   "Carbohydrate metabolism",  0.97, 0.01,
   "OTP", "ko00330",              "Arginine and proline metabolism",                     "Amino acid metabolism",  0.46,    0,
   "OTP", "ko00633",                     "Nitrotoluene degradation", "Xenobiotics biodegradation and metabolism",  0.14, 0.01,
   "OTP", "ko01120", "Microbial metabolism in diverse environments",                                        "??",  0.53,    0,
   "OTP", "ko02024",                               "Quorum sensing",          "Cellular community - prokaryotes",  0.49, 0.01,
   "HTP", "ko00780",                            "Biotin metabolism",      "Metabolism of cofactors and vitamins",  2.05, 0.05,
   "HTP", "ko00860",                         "Porphyrin metabolism",      "Metabolism of cofactors and vitamins",  0.63, 0.01,
   "HTP", "ko00680",                           "Methane metabolism",                         "Energy metabolism",  0.36, 0.06,
   "HTP", "ko00920",                            "Sulfur metabolism",                         "Energy metabolism",  0.26, 0.04,
   "HTP", "ko00910",                          "Nitrogen metabolism",                         "Energy metabolism",  0.66, 0.01,
   "HTP", "ko00650",                         "Butanoate metabolism",                   "Carbohydrate metabolism",  0.68, 0.01,
   "HTP", "ko00630",      "Glyoxylate and dicarboxylate metabolism",                   "Carbohydrate metabolism",  0.94, 0.01,
   "HTP", "ko00330",              "Arginine and proline metabolism",                     "Amino acid metabolism",  0.45,    0,
   "HTP", "ko00633",                     "Nitrotoluene degradation", "Xenobiotics biodegradation and metabolism",   0.1, 0.01,
   "HTP", "ko01120", "Microbial metabolism in diverse environments",                                        "??",  0.52,    0,
   "HTP", "ko02024",                               "Quorum sensing",          "Cellular community - prokaryotes",  0.46, 0.01
  )



p<- ggplot(families, aes(x=family, y=mean, fill=class)) + 
  geom_bar(stat="identity", color="black", width = 0.5,
           position=position_dodge()) + coord_flip() +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.3,position=position_dodge(0.5))
print(p)
# Finished bar plot
p+labs(y = "Relative abundance (%)")+
  theme_classic() +
  theme(legend.position = 'none') +
  theme(axis.text.y = element_text(size = 10)) +
  theme(axis.text.x = element_text(size = 10)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) +
  scale_y_continuous(expand = c(0,0)) +
  scale_fill_manual(name=NULL,
                    breaks = c("OTP", "HTP"),
                    labels = c("OTP", "HTP"),
                    values = c ("#0000ff","#ff0000")) +
  ggsave("HTPOTP_KEGGpathway.png", dpi=1000, dev='png', height=10, width=12)

Sorry. I put the syntax because the "Reprex" package was not available.

I want to rearrange these plots in "genus" order.

I think I may solve with this problem using fct_reorder, but I can't made it.

Thank you

Hi,

Here is one solution:

library(tidyverse)


families <- tibble::tribble(
  ~class,   ~phylum,                                        ~family,                                      ~genus, ~mean,  ~sd,
  "OTP", "ko00780",                            "Biotin metabolism",      "Metabolism of cofactors and vitamins",  1.92, 0.04,
  "OTP", "ko00860",                         "Porphyrin metabolism",      "Metabolism of cofactors and vitamins",  0.68, 0.01,
  "OTP", "ko00680",                           "Methane metabolism",                         "Energy metabolism",  0.47,    0,
  "OTP", "ko00920",                            "Sulfur metabolism",                         "Energy metabolism",  0.18, 0.05,
  "OTP", "ko00910",                          "Nitrogen metabolism",                         "Energy metabolism",   0.7, 0.01,
  "OTP", "ko00650",                         "Butanoate metabolism",                   "Carbohydrate metabolism",  0.71, 0.01,
  "OTP", "ko00630",      "Glyoxylate and dicarboxylate metabolism",                   "Carbohydrate metabolism",  0.97, 0.01,
  "OTP", "ko00330",              "Arginine and proline metabolism",                     "Amino acid metabolism",  0.46,    0,
  "OTP", "ko00633",                     "Nitrotoluene degradation", "Xenobiotics biodegradation and metabolism",  0.14, 0.01,
  "OTP", "ko01120", "Microbial metabolism in diverse environments",                                        "??",  0.53,    0,
  "OTP", "ko02024",                               "Quorum sensing",          "Cellular community - prokaryotes",  0.49, 0.01,
  "HTP", "ko00780",                            "Biotin metabolism",      "Metabolism of cofactors and vitamins",  2.05, 0.05,
  "HTP", "ko00860",                         "Porphyrin metabolism",      "Metabolism of cofactors and vitamins",  0.63, 0.01,
  "HTP", "ko00680",                           "Methane metabolism",                         "Energy metabolism",  0.36, 0.06,
  "HTP", "ko00920",                            "Sulfur metabolism",                         "Energy metabolism",  0.26, 0.04,
  "HTP", "ko00910",                          "Nitrogen metabolism",                         "Energy metabolism",  0.66, 0.01,
  "HTP", "ko00650",                         "Butanoate metabolism",                   "Carbohydrate metabolism",  0.68, 0.01,
  "HTP", "ko00630",      "Glyoxylate and dicarboxylate metabolism",                   "Carbohydrate metabolism",  0.94, 0.01,
  "HTP", "ko00330",              "Arginine and proline metabolism",                     "Amino acid metabolism",  0.45,    0,
  "HTP", "ko00633",                     "Nitrotoluene degradation", "Xenobiotics biodegradation and metabolism",   0.1, 0.01,
  "HTP", "ko01120", "Microbial metabolism in diverse environments",                                        "??",  0.52,    0,
  "HTP", "ko02024",                               "Quorum sensing",          "Cellular community - prokaryotes",  0.46, 0.01
)


# Arrange the data by geneus, then factor the family in genus order
families = families %>% arrange(genus) %>% 
  mutate(family = factor(family, levels = unique(family)))

p<- ggplot(families, aes(x=family, y=mean, fill=class)) + 
  geom_bar(stat="identity", color="black", width = 0.5,
           position=position_dodge()) + coord_flip() +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.3,position=position_dodge(0.5))

# Finished bar plot
p+labs(y = "Relative abundance (%)")+
  theme_classic() +
  theme(legend.position = 'none') +
  theme(axis.text.y = element_text(size = 10)) +
  theme(axis.text.x = element_text(size = 10)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) +
  scale_y_continuous(expand = c(0,0)) +
  scale_fill_manual(name=NULL,
                    breaks = c("OTP", "HTP"),
                    labels = c("OTP", "HTP"),
                    values = c ("#0000ff","#ff0000"))

Created on 2023-02-14 with reprex v2.0.2

1 Like

the order is currently arbitrary but based on the x-axis family value;
start with ordering the data.frame by genus; then you can convert family to a factor type and have that order come in; I added geom_text to show you what the genus are; will help you to adjust if needed to see that.
Once you dont need the geom_text labels just comment that line out


f2 <- families |> arrange(desc(genus)) |> mutate(
  family_fct = as_factor(family)
)
p<- ggplot(f2, aes(x=family_fct, y=mean, fill=class,
                         label=genus)) + 
  geom_text(check_overlap = TRUE,nudge_y = 4) +
2 Likes

Thank you @pieterjanvc,
It worked :slight_smile:

1 Like

Thank you @nirgrahamuk
It worked :slight_smile:

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.