title: R+ggplot: Heatmap. Specific color if value = 'X'
body: <p>I am making some heatmaps in R with ggplot and using RColorBrewer. I have a range of integer values i'm plotting, but some of the values are 'None' and I'd like to colour these a specific colour. Can someone please advise on the most appropriate way to modify my code to add this functionality including adding a second legend that shows 'the new color labelling 'None'? I'm very new to R so not having much luck nutting this one out on my own.</p>
  
  <p>Thanks alot!! :)</p>
  
  <p>Example data</p>
  
  <pre><code>Target  A.tg_t0_rep_A   B.tg_t0_rep_B   C.tg_t0_rep_C   D.tg_t0_rep_D   E.tg_w2_rep_A   F.tg_w2_rep_B   G.tg_w2_rep_C   H.tg_w2_rep_D   I.tg_w4_rep_A   J.tg_w4_rep_B
  1 : 12110501    None    0.5625  0.25    0.5714  None    None    0.5 None    None    0.2857
  1 : 27262099    0.3333  0.8889  0.6667  0.9231  None    None    0.5556  0.6667  None    0.375
  </code></pre>
  
  <p>Code for plot</p>
  
  <pre><code>library(ggplot2)
  library(RColorBrewer)
  
  data &lt;- read.csv('test.csv', header =TRUE)
  rownames(data)=data[,1]
  data_shaped &lt;- data.frame(sample = rep(colnames(data), each = nrow(data)),
                            dmr = rownames(data),
                            methylation_level = unlist(data))
  # remove first n rows from dataset(first col header mistakenly being analysed)
  data_shaped &lt;- data_shaped[-c(1:2), ] 
  
  # set colour palette
  jBuPuFun &lt;- colorRampPalette(brewer.pal(n = 9, "RdBu"))
  paletteSize &lt;- 256
  jBuPuPalette &lt;- jBuPuFun(paletteSize)
  
  # heatmap!
  ggplot(data_shaped, aes(x = sample, y = dmr, fill = methylation_level)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
    geom_tile() +
    scale_fill_gradient2(high = jBuPuPalette[1],
                         mid = jBuPuPalette[paletteSize/2],
                         low = jBuPuPalette[paletteSize],
                         midpoint = (max(data_shaped$methylation_level) +
                                       min(data_shaped$methylation_level)) / 2,
                         name = "methylation_level")
  </code></pre>