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

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

作者: kkkkkkang | 来源:发表于2020-11-28 20:33 被阅读0次

在做物种/功能组成堆叠柱状图时,通常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)
反向排序

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/mpecwktx.html