rggplot2viridis

How to implement two color scales in one ggplot2 graph


I am trying to plot a ggplot2 graph but using two different colour scales, one user defined and one defined by a separate column in my data.frame.

This is easier shown in an example:

gene   chr     start       end    aceth   acvitd disttss    density       fc     color
1   MAP6D1  chr3 183527379 183528379 1.458988 2.190145 15514.0 0.06652449 1.501140 increased
2  MIR6729  chr1  12111873  12112873 1.581880 2.373651 23159.0 0.06244715 1.500525 increased
3 ATP6V0A4  chr7 138440797 138443072 1.517358 2.276540 41006.5 0.06244715 1.500331 increased
4    PRKCQ chr10   6560041   6562565 2.054282 3.081668 61305.0 0.01572513 1.500119 increased
5   UBE2L6 chr11  57327153  57329079 0.972952 1.458081  7687.0 0.12156697 1.498615 unchanged
6  FAM109B chr22  42469873  42471646 1.150138 1.723444   505.5 0.18451594 1.498468 unchanged

I can plot the following graph using this code:

heatmap <-  ggplot(acall, aes(aceth, acvitd,labels=gene, colour = density)) + scale_color_viridis()
heatmap + theme_bw(base_size = 14) # same theme as other graphs
heatmap <- heatmap + geom_point()

Which gives me a nice gradient fill in my graph using the gradient column in my data.frame.

Gradient filled scatter plot

However I then would like to label the genes with fc greater or less than a cutoff in different colors but keep the unchanged genes in this gradient fill.

I can do the first part with:

cols <- c("red" = "red", "unchanged" = "darkgrey", "increased" = "#00B2FF", "decreased" = "#00B2FF")

heatmap <-  ggplot(acall, aes(aceth, acvitd,labels=gene, fill = color))
heatmap <- heatmap + scale_y_continuous(trans = "log") + scale_x_continuous(trans = "log")
heatmap <- heatmap + geom_point(size = 2.5, alpha = 1, na.rm = T, shape = 21, colour = "black")+
  scale_fill_manual(values = cols)

This gives me a nice plot with "increased" and "decreased" genes in one color and "unchanged" in darkgrey. As well as some highlighted points referring to specific genes I named in the "color" column earlier.

non gradient scatter

What I would like to do is plot the "unchanged" genes (so the points in grey in the second plot) in the "color" column with a gradient fill and the rest with my defined color scheme.

Thanks


Solution

  • Since you didn't provide an example with all the combinations of data, I made some up in a similar structure.

    library(dplyr)
    
    df <- tibble(
        gene   = sample.int(5000),
        aceth  = rnorm(5000), 
        acvitd = rnorm(5000)
      ) %>% 
        mutate(
          dens = dnorm(aceth) + dnorm(acvitd),
          chg  = ifelse(density < 0.4 & abs(acvitd) > 2, "changed",   "unchanged"),
          dir  = ifelse(sign(acvitd) == 1,               "increased", "decreased"),
          dir  = replace(dir, gene %in% sample.int(5000, 30), "special annotation")
        )
    
    
     ggplot() + 
       geom_point(data = filter(df, chg == "unchanged"), 
                  aes(aceth, acvitd, color = dens)) + 
       geom_point(data = filter(df, chg == "changed"), 
                  aes(aceth, acvitd, fill = dir), 
                  shape = 22, size = 3) +
       scale_fill_manual(values = c("grey70", "white", "red")) +
       scale_color_viridis() +
       theme_bw()
    

    enter image description here

    You can alter the aesthetics to match what you want, but this is how I would achieve that sort of plot.