graphics - R heatmap with diverging colour palette -
i trying create simple heatmap in r, using diverging colour palette. want use gradient numbers below threshold n designated color (say purple), , numbers above threshold designated color (say orange). further away number threshold, darker color should be.
here sample dataset:
division,col1,col2,col3,col4,col5,col6,col7 division 1,31.9221884012222,75.8181694429368,97.0480443444103,96.295954938978,70.5677134916186,63.0451830103993,93.0396212730557 division 2,85.7012346852571,29.0621076244861,16.9130333233625,94.6443660184741,19.9103083927184,61.9562198873609,72.3791105207056 division 3,47.1665125340223,99.4153356179595,8.51091076619923,79.1276383213699,41.915355855599,7.45079894550145,24.6946100145578 division 4,66.0743870772421,24.6163331903517,78.694460215047,42.04714265652,50.2694897353649,73.0409651994705,87.3745442833751 division 5,29.6664374880493,35.4036891367286,19.2967326845974,5.48460693098605,32.4517334811389,15.5926876701415,76.0523204226047 division 6,95.4969164915383,8.63230894319713,61.7535551078618,24.5590241160244,25.5453423131257,56.397921172902,44.4693325087428 division 7,87.5015622004867,28.7770316936076,56.5095080062747,34.6680747810751,28.1923673115671,65.0204187724739,13.795713102445 division 8,70.1077231671661,72.4712177179754,38.4903231170028,36.1821102909744,97.0875509083271,17.184783378616,78.2292529474944 division 9,47.3570406902581,90.2257485780865,65.6037972308695,77.0234781783074,25.6294377148151,84.900529962033,82.5080851092935 division 10,58.0811711959541,0.493217632174492,58.5604055318981,53.5780876874924,9.12552657537162,20.313960686326,78.1371118500829 division 11,34.6708688884974,76.711881859228,22.6064443588257,22.1724311355501,5.48891355283558,79.1159523651004,56.8405059166253 division 12,33.6812808644027,44.1363711375743,70.6362190190703,3.78900407813489,16.6075889021158,9.12654218263924,39.9711143691093
here simple snippet produce heatmap above data
data <- read.csv("dataset.csv", sep=",") row.names(data) <- data$division data <- data[,2:7] data_matrix <- data.matrix(data) heatmap(data_matrix, rowv=na, colv=na, col = heat.colors(256), scale="column", margins=c(5,10))
how can modify above code produce:
- a color gradient (orange) numbers above 50 (darker further number 50)
- a color gradient (purple) numbers below 50 (darker further number 50)
- nice have (but optional) write number value in grid cell
- nice have (but optional), use different color grid cell threshold number (50 in case)
[[edit]]
i have seen question on so, seems similar. answer uses ggplot (which have no experience of), , have far, been unable adapt ggplot solution more complicated data.
this should of way. (note you'll need set scale="none"
if want plotted colors correspond actual (rather rescaled) values of cells).
ncol <- 100 ## make vector n colors cols <- rcolorbrewer:::brewer.pal(11,"puor") # or c("purple","white","orange") rampcols <- colorramppalette(colors = cols, space="lab")(ncol) rampcols[(n/2) + 1] <- rgb(t(col2rgb("green")), maxcolorvalue=256) ## make vector n+1 breaks rampbreaks <- seq(0, 100, length.out = ncol+1) ## try out heatmap(data_matrix, rowv = na, colv = na, scale="none", col = rampcols, breaks = rampbreaks)
edit
for finer control on placement of threshold, i'd suggest creating 2 separate palettes -- 1 values less threshold , 1 values above threshold -- , "suturing" them together. try this, playing around different values min
, max
, thresh
, etc.:
nhalf <- 50 min <- 0 max <- 100 thresh <- 50 ## make vector of colors values below threshold rc1 <- colorramppalette(colors = c("purple", "white"), space="lab")(nhalf) ## make vector of colors values above threshold rc2 <- colorramppalette(colors = c("white", "orange"), space="lab")(nhalf) rampcols <- c(rc1, rc2) ## in example, line sets color values between 49 , 51. rampcols[c(nhalf, nhalf+1)] <- rgb(t(col2rgb("green")), maxcolorvalue=256) rb1 <- seq(min, thresh, length.out=nhalf+1) rb2 <- seq(thresh, max, length.out=nhalf+1)[-1] rampbreaks <- c(rb1, rb2) heatmap(data_matrix, rowv = na, colv = na, scale="none", col = rampcols, breaks = rampbreaks)
Comments
Post a Comment