如何在geom_tile ggplot中保留图块的顺序 [英] How to preserve the order of tiles in geom_tile ggplot

查看:152
本文介绍了如何在geom_tile ggplot中保留图块的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制geom_tile绘图,但很难保留瓷砖的顺序.这是一个示例数据集

I am try to make geom_tile plot and having difficulty trying to preserve the order of the tiles. Here is an example dataset

head(data2.2)
      wt_mt_up                                         GO ont
1285 10.692307                          proline transport  BP
784  10.319457        salicylic acid biosynthetic process  BP
743   9.550895               systemic acquired resistance  BP
1729  8.563280   response to endoplasmic reticulum stress  BP
841   7.910318 defense response, incompatible interaction  BP
1823  7.765033              defense response to bacterium  BP

p2 <- ggplot(data2.2, aes(x=ont, y=GO, fill = wt_mt_up)) + 
      geom_tile() +
      scale_fill_gradient(legend_title, low="white", high="red") + theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12)) + theme(axis.text.y = element_text(hjust = 1, size = 12))

p2

正如您在输出图中所看到的,尽管GO =脯氨酸转运"的最大值为10.692307,但它位于图的中间.其他的也一样.理想情况下,我希望它们从高到低堆叠.

As you can see in the output plot, eventhough GO = "proline transport" has a max value of 10.692307, it is somewhere in the middle of the plot. Same for the others. I ideally want them to stack from high to low values.

推荐答案

以下是上述问题的答案

p2 <- ggplot(data2.2, aes(x=ont, y=reorder(GO, wt_mt_up), fill = wt_mt_up)) + 
      geom_tile() +
      scale_fill_gradient(legend_title, low="white", high="red") +        
      theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12)) +  
      theme(axis.text.y = element_text(hjust = 1, size = 12))
p2

这篇关于如何在geom_tile ggplot中保留图块的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆