Custom "scale_color_gradient2" in ggplot2

I would like to use my own color in my graphic image. I am very new to ggplot2, so I looked at his manual from here and tried to repeat some things; but I couldn’t figure out how to provide a color panel, like for a graphic plot.

library(reshape2) library(ggplot2) #my specific color list mycol <- vector(length=512, mode = "numeric") for (k in 1:256) mycol[k] <- rgb(255, k - 1, k - 1, maxColorValue=255) for (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") # the code does not really use my color bar 

* Error in the block (tic_pos.c, "mm"): 'x' and 'units' must have a length> 0 *

+4
source share
2 answers

Just to clarify @Didzis answer, which works, but may not create the gradient you're looking for ...

"midpoint" refers to the numeric value by which you want the color specified in the "middle" to be displayed. Thus, instead of setting the “midpoint” argument to 256 (which is outside the range of value , which is the color you are painting), it is wise to set it to a value somewhere in the middle of the range of values ​​that you are coloring, otherwise you will not use the whole gradient that you specified with low and high, which defeats the goal of scale_color_gradient2 . The exact value depends on what you are trying to convey visually, but usually medium or medium is used. Here I edited the @Didzis code with a "midpoint" set to the median 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 a graph with a much wider gradient:

enter image description here

+3
source

I think you should change the values ​​for the low= , mid= and high= scale_fill_gradient2() in scale_fill_gradient2() . For low= I used the first value of mycol , for high= last value of mycol and for mid= value of 256. was used. (Average). midpoint= also changed to 256, as this is the middle of your number of colors.

 ggplot(melt(volcano), aes(x=Var1, y=Var2, fill=value)) + geom_tile() + scale_fill_gradient2(low = "#0000FF", mid = "#FFFFFF", high ="#FF0000", midpoint = 256.5, space = "rgb", guide = "colourbar") 

enter image description here

0
source

Source: https://habr.com/ru/post/1208699/


All Articles