How to align order assignment in ggplot2

2023, Happy New Year !

Currently, I am trying to draw a taxa bar plot using "ggplot2" package.

Actually, my data aligned by "Day" factor.
However, after that I want to align taxa order by LDA score ascend.
As you can see in my result figure, it did not aligned LDA score value.
Please help my code.
I should have used the "Reprex" package, however, it did not work with some errors. Therefore, I made my code a little messy.

Thank you for the expert's help.

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

Lefse.result <- tibble::tribble(
~Pathway, ~LogMaxMean, ~Class, ~LDA, ~pValue, ~Day,
"f__Prevotellaceae_D1", 4.152, "Healthy", 4.248, 0.037, "D-1",
"g__Alloprevotella_D1", 4.152, "Healthy", 4.256, 0.037, "D-1",
"f__Butyricicoccaceae_D1", 4.759, "Healthy", 4.469, 0.046, "D-1",
"g__Butyricicoccus_D1", 4.759, "Healthy", 4.48, 0.046, "D-1",
"g__Ruminococcus_D1", 3.454, "Healthy", 4.78, 0.046, "D-1",
"f__Ruminococcaceae_D1", 5.02, "Healthy", 4.793, 0.046, "D-1",
"f__Rikenellaceae_D3", 3.233, "Healthy", 3.856, 0.046, "D-3",
"g__Alistipes_D3", 3.233, "Healthy", 3.863, 0.046, "D-3",
"f__Oscillospiraceae_D3", 3.485, "Healthy", 4.454, 0.046, "D-3",
"g__Pseudoflavonifractor_D3", 3.485, "Healthy", 4.482, 0.046, "D-3",
"g__Faecalibacterium_D3", 4.887, "Healthy", 4.608, 0.05, "D-3",
"g__Subdoligranulum_D3", 5.066, "Healthy", 4.784, 0.046, "D-3",
"f__Ruminococcaceae_D3", 5.301, "Healthy", 5.027, 0.05, "D-3",
"f__Enterococcaceae_D7", 5.087, "Rota", 4.673, 0.05, "D-7",
"g__Enterococcus_D7", 5.087, "Rota", 4.677, 0.05, "D-7"
)

Lefse.result <-read.csv("raw_data/Dall_lefse.csv", header=TRUE)
Lefse.result %>%
filter (LDA > 2.0) %>%
mutate (LDA = if_else(Class == "Healthy", -1*LDA, LDA),
Pathway = fct_reorder(Pathway, Day),
label_x = if_else(Class == "Healthy", 0.25, -0.25),
label_hjust = if_else(Class == "Healthy", 0, 1)) %>%
ggplot(aes(x=LDA, y=Pathway, fill=Class, width = 0.5)) +
geom_col() +
labs(y="Pathway", x="LDA Score (log 10)") +
scale_x_continuous(limits = c(-5.5, 5.5), breaks = seq(-5.5, 5.5, by=1)) +
scale_fill_manual(name=NULL,
breaks = c("Healthy", "Rota"),
labels = c("Healthy", "Rota"),
values = c ("#F47B0F", "#0101FF")) +
theme_classic()

123.pdf (64.7 KB)

I think you need fct_reorder(Pathway, LDA) .

1 Like

please try to use reorder()
please check the code where i mentioned #update

ggplot(aes(x=LDA, y=reorder(Pathway, LDA), #update
fill=Class, width = 0.5)) +
geom_col() +
labs(y="Pathway", x="LDA Score (log 10)") +
scale_x_continuous(limits = c(-5.5, 5.5), breaks = seq(-5.5, 5.5, by=1)) +
scale_fill_manual(name=NULL,
breaks = c("Healthy", "Rota"),
labels = c("Healthy", "Rota"),
values = c ("#F47B0F", "#0101FF")) +
theme_classic()
1 Like

Thank you for jrkrideau and jkatam.

However, while the LDA values worked in ascending order, the "Day" factor was not being applied again.

Taxabar.pdf (60.2 KB)

did you try the below update in the ggplot, please try

y=reorder(Pathway, Day)

Yes I have changed the code as below.

The result figure is same as first figure I attached.

Lefse.result %>%
filter (LDA > 2.0) %>%
mutate (LDA = if_else(Class == "Healthy", -1*LDA, LDA),
Pathway = fct_reorder(Pathway, Day),
label_x = if_else(Class == "Healthy", 0.25, -0.25),
label_hjust = if_else(Class == "Healthy", 0, 1)) %>%
ggplot(aes(x=LDA, y=reorder(Pathway, Day), fill=Class, width = 0.5)) +
geom_col() +
labs(y="Pathway", x="LDA Score (log 10)") +
scale_x_continuous(limits = c(-5.5, 5.5), breaks = seq(-5.5, 5.5, by=1)) +
scale_fill_manual(name=NULL,
breaks = c("Healthy", "Rota"),
labels = c("Healthy", "Rota"),
values = c ("#F47B0F", "#0101FF")) +
theme_classic()

This topic was automatically closed 42 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.