76935

Heatmap with customized color scale bar for values below and above thresholds

Question:

I would like to make an heatmap in R using pheatmap with the colors green, black and red and using a range in the legend from -2 to 2, here is the code that I used:

library(pheatmap) my_palette <- colorRampPalette(c("green", "black", "red"))(n = 201) colors = c(seq(as.numeric(-2),-0.01,length=100), 0, seq(0.01,as.numeric(2),length=100)) pheatmap(mFilt_annot_sort_matrix, color = my_palette, breaks = colors, scale = "none", cluster_rows = F, cluster_cols = F, margin = c(5,5))

The problem is that I would like the values less than -2 in green an greater than 2 in red while with my solution these values are white, could you help me?

Answer1:

You need to set the break-point to be at -2 and +2 and make a gradient in between. You also need to set the color for less than minimum and greater than maximum desired values. Look below;

library(pheatmap) colors <- c(min(mFilt_annot_sort_matrix),seq(-2,2,by=0.01),max(mFilt_annot_sort_matrix)) my_palette <- c("green",colorRampPalette(colors = c("green", "black", "red")) (n = length(colors)-3), "red") pheatmap(mFilt_annot_sort_matrix, color = my_palette, breaks = colors, scale = "none", cluster_rows = F, cluster_cols = F, margin = c(5,5))

Example using a normal data;

Using <strong><em><a href="https://gist.github.com/tomhopper/19f07fc96db8d149b24f" rel="nofollow">rnorm.within</a></em></strong> function I make the following data-set:

#V1 is random between -4 and 4 #V2 is less than -2 #V3 is greater than 2 df <- data.frame(cbind(rnorm.within(1000, -4, 4) ,rnorm.within(1000,-4,-3), rnorm.within(1000,3,4)))

and applying the procedure above (same breaks and color pallet) for making a heatmap will get:

pheatmap(df, color = my_palette, breaks = colors, scale = "none", cluster_rows = F, cluster_cols = F, margin = c(5,5)) <img class="b-lazy" data-src="https://i.stack.imgur.com/BlWkc.png" data-original="https://i.stack.imgur.com/BlWkc.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Recommend

  • Pass value with action sheet button
  • comparing two Dataframe columns to check if they have same value in python
  • how to Post values from one form to another form?
  • Round up to nearest power of 10
  • How to rotate a PShape around its centre?
  • Portion of label bold - the rest not bold
  • Why is the 100*0.07 equal to 6.9999…? [duplicate]
  • Count the number of non-null values in a Spark DataFrame
  • respondsToSelector - not working
  • Can I disable IE compatibility mode only for content within a ?
  • Plot a table with R
  • Converting float[,] to list in f#?
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Extract data between rows r
  • addressing in assembler
  • Gforce min not supported for character in data.table
  • Neo4j: Filter nodes based on aggregate function
  • force json_encode to create strings
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • Floated image with variable width and heading with background image
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Change multiple background-images with jQuery
  • Fetching methods from BroadcastReceiver to update UI
  • How reduce the height of an mschart by breaking up the y-axis
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • retrieve vertices with no linked edge in arangodb
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • apache spark aggregate function using min value
  • Converting MP3 duration time