r - customized "scale_color_gradient2" in ggplot2 -


i use own specific color in image plot. new in ggplot2 had @ manual here , tried repeat of stuff; couldn't figure out how supply colorbar did graphics plot.

library(reshape2) library(ggplot2)  #my specific color list mycol <- vector(length=512, mode = "numeric") (k in 1:256) mycol[k] <- rgb(255, k - 1, k - 1, maxcolorvalue=255) (k in 257:512) mycol[k] <- rgb(511 - (k - 1), 511 - (k - 1), 255, maxcolorvalue=255) mycol <- rev(mycol) ncolors <- length(mycol)  # graphics plot par(mar = c(5, 13, 1, 6)) image(1:ncol(volcano), 1:nrow(volcano), t(volcano), zlim = c(0, ncolors), col=mycol, axes=false, main="w matrix", sub = "", xlab= "components", ylab="genes") axis(2, at=1:nrow(volcano), labels=row.names(volcano), adj= 0.5, tick=false, las = 1, cex.axis=0.25, font.axis=1, line=-1) axis(1, at=1:ncol(volcano), labels=colnames(volcano), adj= 0.5, tick=false,las = 3, cex=1, cex.axis=0.5, font.axis=1, line=-1)   # ggplot2 library(reshape2) library(ggplot2) library(ez) ggplot(melt(volcano), aes(x=var1, y=var2, fill=value)) + geom_tile() + scale_color_gradient2(low = muted("red"), mid = "white", high = muted("blue"), midpoint = 0, space = "rgb", guide = "colourbar") # code not use color bar 

*error in unit(tic_pos.c, "mm") : 'x' , 'units' must have length > 0*

just clarify @didzis' answer, works may not produce gradient you're looking for...

'midpoint' refers numerical value @ want color specified 'mid' appear. so, instead of setting 'midpoint' argument 256 (which falls outside range of value, vector you're coloring by), it's wise set value somewhere in middle of range of values coloring by, otherwise aren't using entire gradient specified 'low' , 'high', defeats purpose of scale_color_gradient2. exact value depends on trying communicate visually, mean or median used. here, edited @didzis' code 'midpoint' set median of value

v <- melt(volcano) ggplot(v, aes(x=var1, y=var2, fill=value)) +    geom_tile() +    scale_fill_gradient2(low = "#0000ff", mid = "#ffffff", high ="#ff0000",                         midpoint = median(v$value), space = "rgb", guide = "colourbar") 

this gives plot wider gradient:

enter image description here


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -