rggplot2heatmapggnewscale

How to fix y axis order in ggnescale


I'm building a heatmap using different data frame and I'm using ggnewscale to have different color gradients.

The problem is that when I use new_scale("fill") I lost control on the y axis order.

Do you know how to fix it?

This is an explample

library(dplyr)   
library(tidyverse)
library(ggnewscale)

df1<- data.frame(Group = c("A","A","A","B","B","B","C","C","C"),
                  Treatment = c("Control", "Treatment_1", "Treatment_2", "Control", 
                                "Treatment_1","Treatment_2","Control", 
                                "Treatment_1","Treatment_2"),
                  Score = c(12,3,7,4,6,11,2,0,5))
                 
 df1 %>%
   mutate(Score = as.numeric(Score),

          Treatment = factor(Treatment, level = c("Treatment_2", "Control", "Treatment_1" )))%>%
   
 
 ggplot(mapping = aes( x = Group , y = Treatment)) +
  
   
   geom_tile(data = ~ filter(.x,df1$Treatment == "Treatment_1"), mapping = aes(fill = Score)) +
   scale_fill_gradientn(colors = hcl.colors(10, "YlOrBr"), guide = "colourbar") +
   
   new_scale("fill") +
   
   geom_tile(data = ~ filter(.x, df1$Treatment == "Treatment_2"), mapping = aes(fill = Score)) +
   scale_fill_gradientn(colors = rev(hcl.colors(10, "Blues 3")), guide = "colourbar") +
   new_scale("fill") +
   
   geom_tile(data = ~ filter(.x,df1$Treatment == "Control"), mapping = aes(fill = Score)) +
   scale_fill_gradientn(colors = hcl.colors(10, "YlGn"), guide = "colourbar") 

As you can see, even though I set levels for "Treatment" they are not respected.

Thanks for your help


Solution

  • The issue is that unused factor levels are dropped from a discrete scale. And as you use filtered datasets which only contain one of your Treatments the set order is no longer respected. To fix that add a scale_y_discrete(drop = FALSE):

    library(tidyverse)
    library(ggnewscale)
    
    df1 <- data.frame(
      Group = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
      Treatment = c(
        "Control", "Treatment_1", "Treatment_2", "Control",
        "Treatment_1", "Treatment_2", "Control",
        "Treatment_1", "Treatment_2"
      ),
      Score = c(12, 3, 7, 4, 6, 11, 2, 0, 5)
    )
    
    df1 %>%
      mutate(
        Score = as.numeric(Score),
        Treatment = factor(Treatment, level = c("Treatment_2", "Control", "Treatment_1"))
      ) %>%
      ggplot(mapping = aes(x = Group, y = Treatment)) +
      geom_tile(data = ~ filter(.x, df1$Treatment == "Treatment_1"), mapping = aes(fill = Score)) +
      scale_fill_gradientn(colors = hcl.colors(10, "YlOrBr"), guide = "colourbar") +
      new_scale("fill") +
      geom_tile(data = ~ filter(.x, df1$Treatment == "Treatment_2"), mapping = aes(fill = Score)) +
      scale_fill_gradientn(colors = rev(hcl.colors(10, "Blues 3")), guide = "colourbar") +
      new_scale("fill") +
      geom_tile(data = ~ filter(.x, df1$Treatment == "Control"), mapping = aes(fill = Score)) +
      scale_fill_gradientn(colors = hcl.colors(10, "YlGn"), guide = "colourbar") +
      scale_y_discrete(drop = FALSE)
    

    enter image description here