I want to follow this order = genes_order, which I guess is modified by the "stat = identity", overriding it. I don't know how to keep it this way, without the interference of quantitative (if so, because it's not clear) order
The order
genes_order <- c("ABCA1","ABCG1","NR1H2","NR1H3","PPARA",
"PPARD","PPARG","RXRA","RXRB")
df_plot2$gen <- as.factor(df_plot2$gen)
df_plot2 <- as.data.frame(df_plot2)
df_plot2 <- df_plot2[order(df_plot2$gen), ]
The plot: as you can see the order is altered
p1 <- ggplot(data = df_plot2, aes(x = reorder(gen, RQ_gapdh), y = RQ_gapdh, ymin = after_scale(1), fill = Color)) + geom_bar(stat = "identity", aes(fill=Color), width=.5)+
labs(x = "Genes", y = 2^-ddCt~ "value",
title = "Gene expression",
subtitles = "1 year post-intervention")+
guides(fill = F) +
coord_flip()+
facet_wrap(~grup_int, labeller = labeller(grup_int = c("1" = "Olive oil", "2" = "Nuts", "3" = "Low-fat diet")))
This is my df
df_plot2 <-
structure(list(gen = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L,
8L, 9L, 9L, 9L), levels = c("ABCA1", "ABCG1", "NR1H2", "NR1H3",
"PPARA", "PPARD", "PPARG", "RXRA", "RXRB"), class = "factor"),
grup_int = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L,
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L,
3L, 2L, 1L), levels = c("1", "2", "3"), class = "factor"),
time = c("3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3"), RQ_gapdh = c(1.019, 1.218, 1.183,
0.926, 1.132, 1.052, 0.936, 1.117, 0.961, 0.987, 1.182, 0.971,
1.008, 0.985, 0.951, 0.935, 1.149, 1.058, 0.914, 1.01, 0.952,
0.913, 1.134, 1.112, 0.985, 1.074, 1.135), Color = structure(c(2L,
2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L), levels = c("FALSE",
"TRUE"), class = "factor")), row.names = c(8L, 17L, 26L,
9L, 18L, 27L, 5L, 14L, 23L, 4L, 13L, 22L, 1L, 10L, 19L, 2L, 11L,
20L, 3L, 12L, 21L, 6L, 15L, 24L, 7L, 16L, 25L), class = "data.frame")
The issue is that you reorder()
your gen
variable. Drop that and you should be fine. And if you want to reverse the order then could do so by setting limits=rev
in scale_x/y_discrete
.
Note: I dropped the coord_flip
and switched the mapping on x and y instead.
library(ggplot2)
ggplot(data = df_plot2, aes(
x = RQ_gapdh, y = gen,
fill = Color
)) +
geom_col(width = .5) +
scale_y_discrete(limits = rev) +
labs(
y = "Genes", x = 2^-ddCt ~ "value",
title = "Gene expression",
subtitles = "1 year post-intervention"
) +
guides(fill = "none") +
facet_wrap(~grup_int,
labeller = labeller(
grup_int = c(
"1" = "Olive oil",
"2" = "Nuts",
"3" = "Low-fat diet"
)
)
)