ggplot2堆叠柱状图x轴顺序随心所欲|y轴按丰度排序

2020-11-28  本文已影响0人  kkkkkkang

在做物种/功能组成堆叠柱状图时,通常ggplot2默认出图的x轴顺序是按字母或者数字大小排序的

默认x轴顺序

ggplot(mtcars,aes(cyl,disp)) + geom_bar(stat = "identity")
默认x轴顺序

随心所欲排x轴

mtcars$cyl <- factor(mtcars$cyl, levels = c("6","4","8"))
ggplot(mtcars,aes(cyl,disp)) + geom_bar(stat = "identity")
随心所欲排x轴

有时候为了直观地比较两种不同处理之间的物种/功能变化情况,将丰度按照大小排序好一些

不排序

> ggplot(diamonds, aes(cut,price,fill=color)) +geom_bar(stat = "identity") + xlab(NULL)
#这里xlab(NULL)是去掉横轴标题,碍眼吧啦的搁这
不排序

排序

> diamonds$color <- reorder(diamonds$color, diamonds$price)
> ggplot(diamonds, aes(cut,price,fill=color)) +geom_bar(stat = "identity") + xlab(NULL)
重排序

反向排序

> diamonds$color <- reorder(diamonds$color, -diamonds$price)
> ggplot(diamonds, aes(cut,price,fill=color)) +geom_bar(stat = "identity") + xlab(NULL)
反向排序
上一篇 下一篇

猜你喜欢

热点阅读