To order things in ggplot2
, you use factors and specify factor levels as the order. The key thing in your example is that you actually want to order your pathways: your y
variable (Description
), based on your database: your fill
variable (ONTOLOGY
).
A solution could be to:
- specify the database (
ONTOLOGY
) variable as a factor where the levels are the desired order (you could use forcats
for this as Mark suggests)
- arrange your input data by this database factor with
dplyr::arrange
- speficy the pathway (
Description
) variable as a factor where the levels are the final order in which they are
For example:
library(ggplot2)
library(dplyr)
data <- data.frame(
Description = c("A","E","B","I","O","J","K"),
ONTOLOGY = c("BP","MF","BP","MF","CC","BP","CC"),
Count = sample(1:10,7)
)
ggplot(data, aes(Count, Description, fill = ONTOLOGY)) + geom_bar(stat = "identity") +
theme(legend.position = "bottom", axis.title.y = element_blank()) +
geom_text( aes(label = paste(Count)), color = "black", size = 4, hjust = 0.1, position = position_dodge(0.9) )
data$ONTOLOGY <- factor(data$ONTOLOGY, levels = rev(c("BP","MF","CC")))
data <- dplyr::arrange(data, ONTOLOGY)
data$Description <- factor(data$Description, levels = data$Description)
ggplot(data, aes(Count, Description, fill = ONTOLOGY)) + geom_bar(stat = "identity") +
theme(legend.position = "bottom", axis.title.y = element_blank()) +
geom_text( aes(label = paste(Count)), color = "black", size = 4, hjust = 0.1, position = position_dodge(0.9) )
Check the package
forcats
to manually reorder the GO terms https://forcats.tidyverse.org/reference/fct_relevel.html