0

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

user438383
  • 5,716
  • 8
  • 28
  • 43
Chemokine1
  • 17
  • 3
  • 2
    `library(dplyr); GFP_long %>% arrange(desc(variable), -value) %>% mutate(Entrez.Symbol = fct_inorder(Entrez.Symbol))` – Jon Spring Sep 11 '22 at 21:15
  • I tried to run this code, but I gen an error back: ! could not find function "fct_inorder" – Chemokine1 Sep 11 '22 at 22:41
  • Sorry, I should have used `forcats::fct_inorder` since it comes from the `forcats` package. It offers some convenient functions for dealing with factors. – Jon Spring Sep 11 '22 at 23:08
  • Thank you for your help, but it still gives me the same error. ``Error in `mutate()`: ! Problem while computing `Entrez.Symbol = fct_inorder(Entrez.Symbol)`. Caused by error in `fct_inorder()`: ! could not find function "fct_inorder"`` – Chemokine1 Sep 11 '22 at 23:35
  • 2
    Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here. One way of doing this is by using the `dput` function. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Sep 11 '22 at 23:47
  • The code from @Jon Spring, is working fine for me. You can try to run `install.packages("forcats")` and `library(forcats)` prior running the code – Bushidov Sep 12 '22 at 07:09
  • I run the code after running `library(forcats)` but I still get this error: `Error in `mutate()`: ! Problem while computing `Entrez.Symbol = fct_inorder(Entrez.Symbol)`. Caused by error in `fct_inorder()`: ! could not find function "fct_inorder"` – Chemokine1 Sep 12 '22 at 16:37

1 Answers1

0

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")
Jon Spring
  • 55,165
  • 4
  • 35
  • 53