I am trying to setup a custom scale in R. My data ranges in values from -5.4 to +3.6 and I want to center the data around 0 (white). I would like a scale to the data such that I have the same number of gradations above and below 0 (I am shooting for 7 at the moment). The issue that I am having is that I can't get the scaling correct and I am not sure where my problem is.
My code at the moment (source data is in Pastebin link at the bottom):
png('127-2_4_compare_other.png',width = 1200, height = 800, units = "px")
colfunc <- colorRampPalette(c("blue", "white", "red"))
f <- function(m) t(m)[,nrow(m):1]
colorBarz=matrix(seq(-5.5,4,len=15),nrow=1)
colorBarx=1
source("127-2_4.CompareMatrix.txt")
colorBary=seq(-5.4,3.6,len=15)
cus_breaks=c(-5.400, -4.725, -4.050, -3.375, -2.700, -2.025, -1.350, -0.675, 0.45, 0.90, 1.35, 1.80, 2.25, 2.70, 3.15, 3.60)
layout(matrix(c(1,2), 1, 2, byrow = TRUE), widths=c(9,1))
image(f(Compare2and4),axes=FALSE,ylab="Amino acids",xlab="Position",main="Sample 2 vs. 4",col=colfunc(15),breaks=cus_breaks)
axis(1, seq(from = 0, to = 1, by = 0.03703), labels=c(1:11,1:17))
axis(2, seq(from = 0, to = 1, by = 0.0526),labels=rev(c("A","R","N","D","C","E","Q","G","H","I","L","K","M","F","P","S","T","W","Y","V")),las=2)
image(colorBarx,colorBary,colorBarz,col=colfunc(15),axes=FALSE,xlab="",ylab="log(Sample4 / Sample2)",breaks=cus_breaks)
axis(2,las=2)
dev.off()
I am looking for seven evenly split bins above 0 to 3.6 and seven evenly split bins below 0 to -5.4 and I would like 0 to hit in the middle of the white bin. Also if anyone can look over the heatmap code itself to make sure that there are no obvious errors I would greatly appreciate it. Pastebin of the source data
To specify colors of the bar in Barplot in ggplot2, we use the scale_fill_manual function of the ggplot2 package. Within this function, we need to specify a color for each of the bars as a vector. We can use colors using names as well as hex codes.
In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.
The R package ggsci contains a collection of high-quality color palettes inspired by colors used in scientific journals, data visualization libraries, and more.
A problem with your proposed color scale is that the perceptual distance between each point on the scale is not equal. The same color distance refers a larger range of data values on the positive end, compared to the negative end. I would suggest not reinventing the wheel, and making use of the great capabilities of ggplot
.
By default, the diverging color scale will have white as the midpoint at value 0.
For example:
Compare2and4_t <- t(Compare2and4)[,nrow(Compare2and4):1]
am_ac <- c("A","R","N","D","C","E","Q","G","H","I","L","K","M","F","P","S","T","W","Y","V")
pdat <- data.frame(x = 1:nrow(Compare2and4_t),
y = factor(rep(am_ac, each = nrow(Compare2and4_t)), levels = rev(am_ac)),
val = c(Compare2and4_t))
library(ggplot2)
library(scales)
ggplot(pdat, aes(x, y, fill = val)) +
geom_tile() +
scale_fill_gradient2(low = 'darkblue', high = 'darkred',
limits = c(-5.4, 3.6), oob = squish) +
coord_equal(expand = FALSE) +
scale_x_continuous(breaks = unique(pdat$x)) +
theme_classic() +
labs(x = 'Position', y = 'Amino acids')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With