define color gradient for negative and positive values scale_fill_gradientn()

9.9k views Asked by At

R version 3.1.1 (2014-07-10) Platform: i386-w64-mingw32/i386 (32-bit)

I am working on a heatmap with ggplot2() values from -1 to +1. I want to achieve that negative values have a bluish gradiend from darkblue(-1) to lightblue(-0.00000001) and the positive values a redish one from lightred(0.00000001) to darkred(1). Small values should not fade into white, as zeros should have white color and there shall be a visible contrast between 0 and a small value. NA values should be grey.

I tried scale_fill_gradient2() and scale_fill_gradientn() also with rescale().

A similar topic has been discussed, but I coud not solve the problem in my case:

Is it possible to define the "mid" range in scale_fill_gradient2()?

ggplot2 positive and negative values different color gradient

Data (positive and negative):

data.clean <- structure(list(`1` = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
), `2` = c(-0.742791272678673, -0.637008381689233, -0.678722760290557, 
-0.374604178402531, -0.752540076766764, -0.741425721633379, -0.790950497422989, 
-0.822779886175826, -0.696227459496285, -0.698453447846652), 
`3` = c(-0.525688574069743, -0.43137330754352, -0.363377723970944, 
-0.210140400587573, -0.532475351847671, -0.587485278919413, 
-0.683607015861601, -0.630387125338536, -0.494214515339252, 
-0.566343042071197), `4` = c(-0.311771191820642, -0.320141843971631, 
-0.20021186440678, -0.0798166672550588, -0.369634479817365, 
-0.452880524911674, -0.526639098645571, -0.401771184676448, 
-0.278097123757212, -0.367096091610655), `5` = c(-0.247251829329413, 
-0.22468085106383, -0.115738498789346, 0, -0.305938135019192, 
-0.355658903549883, -0.356666267130129, -0.25741406255345, 
-0.0161801962913287, -0.0980209111277072), `6` = c(-0.0459977502311025, 
-0.150870406189555, -0.0659806295399516, 0, -0.200647249190939, 
-0.248640853702502, -0.269487394622238, -0.145798738610008, 
0.889467675181961, -0.0158700522778193), `7` = c(0.122867924528302, 
-0.107079303675048, -0.0558414043583535, 0, -0.143962770627932, 
-0.198013555411349, -0.185704582684086, 0.387784090909091, 
0.639289282146425, 0.410557184750733), `8` = c(0.985358490566038, 
-0.0924822695035461, -0.0304933414043584, 0, -0.088620456084895, 
-0.139027086788281, -0.0479443845139638, 0.715909090909091, 
0.856286570572285, 0.98533724340176), `9` = c(1, -0.0658736299161831, 
-0.0118038740920097, 0, -0.0431499460625674, -0.0776912538755498, 
0.475849731663685, 1, 1, 1), `10` = c(0.692830188679245, 
-0.031063829787234, -0.00579600484261501, 0, -0.0319485211108602, 
-0.0461460811882616, 0.60169946332737, 0.772727272727273, 
0.44955044955045, 0.765591397849462), `11` = c(0.489056603773585, 
-0.00773694390715667, -0.0045475181598063, 0, -0.0309324904041544, 
-0.0235537289398419, 1, 0.574090909090909, 0.0749250749250749, 
0.668621700879765), `12` = c(0.649056603773585, 0.170212765957447, 
0, 0, -0.0238328190461855, -0.0173047804455981, 0.692307692307692, 
0.353977272727273, 0.0784929356357928, 0.617790811339198), 
`13` = c(0.78188679245283, 0.723404255319149, 0, 0, -0.00752615338300595, 
0.559210526315789, 0.471124031007752, 0.298295454545455, 
0.096118167546739, 0.551319648093842), `14` = c(0.679245283018868, 
0.791489361702128, 0, 0, -0.00150523067660119, 0.785526315789474, 
0.453488372093023, 0.130681818181818, 0.121735407449693, 
0.183773216031281), `15` = c(0.414943396226415, 1, 0, 0, 
-0.000376307669150297, 0.793421052631579, 0.433810375670841, 
0, 0, 0.183773216031281), `16` = c(0.250415094339623, 0, 
0, 0, 1, 1, 0.509838998211091, 0, 0, 0.899315738025415), 
`17` = c(0, 0, 0, 0, 0, 0.720789473684211, 0.447227191413238, 
0, 0, 0), `18` = c(0, 0, 0, 0, 0, 0.868421052631579, 0.374955277280859, 
0, 0, 0), `19` = c(0, 0, 0, 0, 0, 0, 0.584376863446631, 0, 
0, 0), `20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Type = structure(1:10, .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", 
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", 
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", 
"43", "44", "45", "46", "47", "48", "49", "50", "51", "52", 
"53"), class = "factor")), .Names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "Type"), row.names = c(NA, 10L), class = "data.frame")

Data (only positive):

data.clean <- structure(list(`1` = c(0.709932715640856, 0.943366065311715, 
0.954871794871795, 1, 0.810404161664666, 1, 0.349208911850178, 
0.502441037735849, 1, 0.948646446923173), `2` = c(0.657238583719391, 
0.919799040876913, 1, 0.826392095299658, 1, 0.899561128526646, 
0.395221181788828, 0.838679245283019, 0.921675990953458, 1), 
`3` = c(0.838710566953452, 1, 0.747511312217195, 0.601856127066211, 
0.706962785114046, 0.737993730407523, 0.688440426218922, 
1, 0.665158909653613, 0.857250281990224), `4` = c(0.881866313699537, 
0.959123087462891, 0.607541478129713, 0.392464678178964, 
0.483393357342937, 0.629968652037618, 1, 0.826297169811321, 
0.46194500654684, 0.719388394535656), `5` = c(0.78590337524818, 
0.9762502854533, 0.522956259426848, 0.192815587773571, 0.365266106442577, 
0.482445141065831, 0.870649015175977, 0.612240566037736, 
0.360195214855374, 0.618310565233739), `6` = c(0.864493712772998, 
0.816396437542818, 0.477828054298643, 0.112845138055222, 
0.256982793117247, 0.420062695924765, 0.71139812721989, 0.575471698113208, 
0.260135698131175, 0.452390650457451), `7` = c(1, 0.833523635533227, 
0.4, 0.0793702096223105, 0.155822328931573, 0.347586206896552, 
0.524701323861802, 0.430672169811321, 0.175979050113082, 
0.326356686301542), `8` = c(0.738335539377895, 0.737611326786938, 
0.352941176470588, 0.0473266229568751, 0.111644657863145, 
0.310971786833856, 0.384780970831988, 0.311320754716981, 
0.0910605880252351, 0.211743326231357), `9` = c(0.521178027796162, 
0.650833523635533, 0.364705882352941, 0.045064179517961, 
0.127450980392157, 0.351097178683386, 0.27788182111721, 0.265330188679245, 
0.0636828948934651, 0.145412959017421), `10` = c(0.412599272005295, 
0.554921214889244, 0.407843137254902, 0.0367069904885031, 
0.0789915966386555, 0.273981191222571, 0.256161877085351, 
0.173349056603774, 0.0484942268777526, 0.161204411580399), 
`11` = c(0.322634017207148, 0.364238410596026, 0.378280542986425, 
0.0267799427463293, 0.0424169667867147, 0.295297805642633, 
0.267807555699064, 0.13561320754717, 0.0535650517795501, 
0.129715503195889), `12` = c(0.251282263401721, 0.231788079470199, 
0.313725490196078, 0.0221627112383415, 0.0256102440976391, 
0.150470219435737, 0.23680981595092, 0.148372641509434, 0.0664992262825854, 
0.0552230855996992), `13` = c(0.173726009265387, 0.147841973053209, 
0.251674208144796, 0, 0, 0.058307210031348, 0.139920353029814, 
0.202830188679245, 0.0614212593738841, 0.0547687680160421
), `14` = c(0.194200860357379, 0.115734185887189, 0.242413273001508, 
0, 0, 0, 0.102034226670972, 0.101415094339623, 0.017259850017855, 
0.062131846096002), `15` = c(0.162213214206927, 0, 0.135746606334842, 
0, 0, 0, 0.0565062964158863, 0.0790683962264151, 0, 0.0274157162551698
), `16` = c(0.0654230090447827, 0, 0.0588235294117647, 0, 
0, 0, 0.0742654181465935, 0.122641509433962, 0, 0), `17` = c(0, 
0, 0.107119155354449, 0, 0, 0, 0, 0.188679245283019, 0, 0
), `18` = c(0, 0, 0.110316742081448, 0, 0, 0, 0, 0.265070754716981, 
0, 0), `19` = c(0, 0, 0.2473604826546, 0, 0, 0, 0, 0.220518867924528, 
0, 0), `20` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Type = structure(1:10, .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor")), .Names = c("1",     "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "Type"), row.names = c(NA, 10L), class = "data.frame")

The code is using ggplot2,reshape2andRColorBrewer. The following code worked quite sufficiant, BUT as soon as i have only positive values, the positive values will also be colored bluish

data.m  <- melt(data.clean, id = "Type") 

bl <- colorRampPalette(c("navy","royalblue","lightskyblue"))(200)                      
re <- colorRampPalette(c("mistyrose", "red2","darkred"))(200)

ggplot(data.m, aes(x=variable, y=Type, fill=value)) +
  geom_tile(colour = "grey92", size = 0.5) +
  scale_fill_gradientn(colours=c(bl,"white", re), na.value = "grey98")

I tried the rescale function, but i can not handle it. I do not get any good results.

ggplot(data.m, aes(x=variable, y=Type, fill=value)) +
  geom_tile(colour = "grey92", size = 0.5) +
  scale_fill_gradientn(colours=c(bl,"white", re), na.value = "grey98",
  values = rescale(c(-1,-0.001,0,0.001,1)))

I hope you can help me!

Thanks a lot!

1

There are 1 answers

0
thothal On BEST ANSWER

You need to specify the limits argument in scale_fill_gradientn:

data.m <- melt(data.clean, id = "Type")
p <- ggplot(data.m, aes(x = variable, y = Type, fill = value) + geom_tile() +
         scale_fill_gradientn(colours=c(bl,"white", re), na.value = "grey98",
         limits = c(-1, 1)) 
p
p %+% transform(data.m, value = abs(value))

Will give the two following graphs:

All Values Positive Values