R ggplot2 中reorder()如何降序

喜大普奔,终于解决了reorder()降序排序的问题;
举例说明一下:
首先我有两列数据:第一列是样品名,第二列是对应数值。


image.png

先画个简单的barplot:

library(ggplot2)
data = read.table("test.txt",header=F)
ggplot(data,aes(x=V1,y=V2))+ geom_bar(stat = "identity")

如下图:


image.png

我想将x轴排序,可以提前将数据排好序之后再画图,但是这里用reorder()函数直接根据y值的大小排序:

ggplot(data,aes(x=reorder(V1,V2),y=V2))+ geom_bar(stat = "identity")

如图:


image.png

但是reorder如何从大到小排呢?
研究了好久,比如加了reverse=F/T,加了decrease=F/T,等等,都不行,终于发现原来只要加个“-”就可以:

ggplot(data,aes(x=reorder(V1,-V2),y=V2))+ geom_bar(stat = "identity")

图:


image.png

图不怎么好看,只是为了演示reorder从大到小排序。

你可能感兴趣的:(R ggplot2 中reorder()如何降序)