简体   繁体   中英

Reorder bars in geom_bar ggplot2 by value

I am trying to make a bar-plot where the plot is ordered from the miRNA with the highest value to the miRNA with the lowest. Why does my code not work?

> head(corr.m)

        miRNA         variable value
1    mmu-miR-532-3p      pos     7
2    mmu-miR-1983        pos    75
3    mmu-miR-301a-3p     pos    70
4    mmu-miR-96-5p       pos     5
5    mmu-miR-139-5p      pos    10
6    mmu-miR-5097        pos    47

ggplot(corr.m, aes(x=reorder(miRNA, value), y=value, fill=variable)) + 
  geom_bar(stat="identity")

Your code works fine, except that the barplot is ordered from low to high. When you want to order the bars from high to low, you will have to add a - sign before value :

ggplot(corr.m, aes(x = reorder(miRNA, -value), y = value, fill = variable)) + 
  geom_bar(stat = "identity")

which gives:

在此处输入图片说明


Used data:

corr.m <- structure(list(miRNA = structure(c(5L, 2L, 3L, 6L, 1L, 4L), .Label = c("mmu-miR-139-5p", "mmu-miR-1983", "mmu-miR-301a-3p", "mmu-miR-5097", "mmu-miR-532-3p", "mmu-miR-96-5p"), class = "factor"),
                         variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "pos", class = "factor"),
                         value = c(7L, 75L, 70L, 5L, 10L, 47L)),
                    class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))

Aside from what @Jaap answered, there are two other ways to order the plot:

1: By using desc argument for value:

ggplot(corr.m, aes(x = reorder(miRNA, desc(value)), y = value, fill = variable)) + 
   geom_bar(stat = "identity")

2: By releveling the miRNA factor and omitting reorder argument:

corr.m %>%
   arrange(desc(value)) %>%
   mutate(miRNA = factor(miRNA, levels = unique(miRNA))) %>% 
ggplot(aes(x = miRNA, y = value, fill = variable)) + 
   geom_bar(stat = "identity") 

Youu can reorder variables along the x-axis using the reorder() function. However, sometimes ggplot doesn't listen to reorder. This is usually because the additional layers to the plot are added in the wrong order. If you have a complex plot and reorder() isn't affecting the outcome, try temporarily removing or muting the other functions, then adding them back one-by-one.

ggplot(aes(x=reorder(myx, -myy), y=myy), data=mydf) + geom_bar(stat="identity")

Another option is creating the variable as a factor where the factor levels are in decreasing order based on your value variable.

decreasing = TRUE

library(ggplot2)
# Create factor column with decreasing order TRUE
corr.m$miRNA <- factor(corr.m$miRNA, levels = corr.m$miRNA[order(corr.m$value, decreasing = TRUE)])

ggplot(corr.m, aes(x=miRNA, y=value, fill=variable)) + 
  geom_bar(stat="identity") 

Created on 2022-08-19 with reprex v2.0.2

decreasing = FALSE

library(ggplot2)
# Create factor column with decreasing order FALSE
corr.m$miRNA <- factor(corr.m$miRNA, levels = corr.m$miRNA[order(corr.m$value, decreasing = FALSE)])

ggplot(corr.m, aes(x=miRNA, y=value, fill=variable)) + 
  geom_bar(stat="identity")

Created on 2022-08-19 with reprex v2.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM