rggplot2geom-tile

sorting data in geom_tile()


My data:

GFP_mywide<-bothdata[1:10,c(2,3,4)]

GFP_long<-melt(GFP_mywide, id =c('Entrez.Symbol'))

looks like this:

>GFP_long
Entrez.Symbol    variable value

1         TRIP11   GFP_my 1.015

2          SIN3B   GFP_my 0.336

3          SF3B1   GFP_my 0.315

4         PSMD14   GFP_my 0.254

5          RAD51   GFP_my 0.286

6          BARD1   GFP_my 0.157

7          BRCA1   GFP_my 0.275

8          BRCA1   GFP_my 0.230

9        U5200KD   GFP_my 0.772

10         SETD5   GFP_my 0.364

11        TRIP11 GFP_wide 0.020

12         SIN3B GFP_wide 0.055

13         SF3B1 GFP_wide 0.071

14        PSMD14 GFP_wide 0.102

15         RAD51 GFP_wide 0.109

16         BARD1 GFP_wide 0.139

17         BRCA1 GFP_wide 0.146

18         BRCA1 GFP_wide 0.146

19       U5200KD GFP_wide 0.151

20         SETD5 GFP_wide 0.179

I want to create a heatmap when the values are sorted according to GFP_wide, so in the plot I will see the green becoming red for the GFP_wide, and GFP_my will be ordered by the same Entrez.Symbol. Until now I have a heatmap but can't find a way to sort the values based on GFP_wide. How do I do that?

This is my code and result:

ggplot(GFP_long, aes(x=Entrez.Symbol,y=variable,fill=value)) + 

  geom_tile(aes(fill = value))+

  scale_fill_gradient(low="red", high="green")

enter image description here


Solution

  • Using the data from your other question:

    library(dplyr); library(forcats) # please check these load successfully
    GFP_long %>% 
      arrange(desc(variable), -value) %>% 
      mutate(Entrez.Symbol = fct_inorder(Entrez.Symbol)) %>%
    ggplot(aes(x=Entrez.Symbol,y=variable,fill=value)) + 
      geom_tile(aes(fill = value))+
      # I'm modifying here to show the top row is ordered; hard to see in original
      scale_fill_gradient2(low="red", mid = "gray80", high="green", midpoint = 0.2)
    

    enter image description here

    Data: how to order column by a different column value r

    GFP_long<-data.frame(Entrez.Symbol<-c("TRIP11","SIN3B","SF3B1","PSMD14","RAD51","BARD1", 
                                     "BRCA1","BRCA1","U5200KD","SETD5","TRIP11","SIN3B",  
                                     "SF3B1","PSMD14","RAD51","BARD1","BRCA1","BRCA1",
                                     "U5200KD","SETD5"),
                         variable<-c("GFP_my","GFP_my","GFP_my","GFP_my","GFP_my","GFP_my","GFP_my",  
                               "GFP_my","GFP_my","GFP_my","GFP_wide","GFP_wide","GFP_wide","GFP_wide",
                                "GFP_wide","GFP_wide","GFP_wide","GFP_wide","GFP_wide","GFP_wide"),
                          value<-c(1.015,0.336,0.315,0.254,0.286,0.157,0.275,0.230,0.772,0.364,
                             0.020,0.055,0.071,0.102,0.109,0.139,0.146,0.146,0.151,0.179))
    
    colnames(GFP_long)<-c("Entrez.Symbol","variable","value")