简体   繁体   English

按值对 geom_bar ggplot2 中的条形重新排序

[英]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.我正在尝试制作一个条形图,其中 plot 从具有最高valuemiRNA到具有最低值的miRNA排序。 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 :当您想从高到低对条形进行排序时,您必须在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:除了@Jaap 的回答之外,还有另外两种方法可以对情节进行排序:

1: By using desc argument for value: 1:通过使用desc参数作为值:

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: 2:通过重新调整miRNA因子并省略reorder参数:

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. 您可以使用reorder()函数沿x轴重新排序变量。 However, sometimes ggplot doesn't listen to reorder. 但是,有时ggplot不会听重新排序。 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. 如果您有复杂的情节并且reorder()不影响结果,请尝试暂时删除或静音其他功能,然后逐个添加它们。

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.另一种选择是将变量创建为一个factor ,其中因子levels根据您的值变量按降序排列。

decreasing = TRUE减少 = 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使用reprex v2.0.2创建于 2022-08-19

decreasing = FALSE减少 = 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使用reprex v2.0.2创建于 2022-08-19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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