3

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_plot

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.

upendra
  • 2,141
  • 9
  • 39
  • 64
  • You need to tell the order to ggplot, either explicitly by using `reorder` (maybe y=reorder(GO, wt_mt_up)) or implicitly by reordering the factor in your data. – Heroka Oct 30 '15 at 06:56
  • @Heroka awesome it worked....thanks for the tip – upendra Oct 30 '15 at 07:04

1 Answers1

4

Here is the answer for the above problem

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
upendra
  • 2,141
  • 9
  • 39
  • 64