定制的“scale_color_gradient2”在ggplot2中 [英] customized "scale_color_gradient2" in ggplot2

查看:1767
本文介绍了定制的“scale_color_gradient2”在ggplot2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的图片情节中使用自己的特定颜色。我在ggplot2中很新,所以从这里看看它的手册并试图重复一些东西;但我无法弄清楚如何为图形绘制提供色条。

  library(reshape2)
library(ggplot2)

#my特定颜色列表
mycol< - vector(length = 512,mode =numeric)
for(k in 1:256 )对于(k在257中为512)mycol [k] <-rgb(255,k-1,k-1,maxColorValue = 255)
1),511-(k-1),255,maxColorValue = 255)
mycol < - rev(mycol)
ncolors< - length(mycol)

# (火山),t(火山),zlim = c(火山),1:ncol(火山),1:ncol(火山)图形图b $ b par(mar = c(5,13,​​1,6) 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 (低=静音(红色),中等=白色,高=静音(蓝色),中点= 0,空间=rgb,指导=colourbar)#代码并不真正使用我的颜色bar

*单位错误(tic_pos.c,mm):'x'和'units '必须有长度> 0 *

解决方案

只是为了澄清@Didzis的答案,它的工作原理可能不会产生渐变,重新寻找......



'midpoint'是指您希望'mid'指定的颜色出现的数值。因此,不要将中点参数设置为256(不在的范围之外,这是您着色的向量),因此设置它是明智的在某个值的某个值的某个范围内,您正在着色的值的范围内,否则,您没有使用您用'低'和'高'指定的整个梯度,这违背了 scale_color_gradient2 。确切的价值取决于你想要通过视觉进行沟通,但通常使用平均数或中位数。在这里,我编辑了@Didzis的'midpoint'代码设置为值的中间值

  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)

这给出了一个更宽的渐变图:


I would like to use my own specific color in my image plot. I am very new in ggplot2 so had a look at its manual from here and tried to repeat some of the stuff; but I couldn't figure out how to supply my colorbar as I did for the graphics 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 unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0*

解决方案

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

'midpoint' refers to the numerical value at which you want the color specified by 'mid' to appear. So, instead of setting the 'midpoint' argument to 256 (which falls outside the range of value, which is the vector you're coloring by), it's wise to set it to a value somewhere in the middle of the range of values you are coloring by, otherwise you aren't using the entire gradient you specified with 'low' and 'high', which defeats the purpose of scale_color_gradient2. The exact value depends on what you are trying to communicate visually, but usually the mean or median is used. Here, I edited @Didzis' code with 'midpoint' set to the 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 a plot with a much wider gradient:

这篇关于定制的“scale_color_gradient2”在ggplot2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆