简体   繁体   English

ggplot2 热图,在图形之间具有固定比例的颜色条

[英]ggplot2 heatmap with fixed scale colorbar between graphs

I need to draw 3 different plots setting the same scale range color.我需要绘制 3 个设置相同比例范围颜色的不同图。 I have 3 matrices with different range of values.我有 3 个具有不同值范围的矩阵。

For example:例如:

range(matrixA) 
# 0.60 0.85  
range(matrixB) 
# 0.65 0.95  
range(matrixA) 
# 0.5 1.0

I would like to have the same color fill for the plots.我想为地块填充相同的颜色。 For example, for all 0.8 value in the difference plots, if in the first plot 0.8 orange, I want all 0.8 value in different graphs to be the same orange.例如,对于差异图中的所有 0.8 值,如果在第一个图中 0.8 橙色,我希望不同图中的所有 0.8 值都是相同的橙色。

My problem in this moment is:我现在的问题是:

In the first plot, the color of the max value is red, then the value 0.85 is red.在第一个图中,最大值的颜色是红色,那么值 0.85 就是红色。

In the second plot, the max value is red but in this case the max value is 0.95 and the problem arises.在第二个图中,最大值为红色,但在本例中最大值为 0.95,问题就出现了。

My code:我的代码:

mat.melted <- melt(matrixA)
colnames(mat.melted) <- c("p","c","v")
p <- ggplot(mat.melted, aes(x=c,y=p,fill=v) + 
         geom-tile() + 
         scale_fill_gradintn(limits = c(min(as.vector(matrixA)), max(as.vector(matrixA))), 
                             colors = c("yellow","orange","red"))

You need to set the same range (limits of color bar) for all of them and also specify the colors.您需要为所有这些设置相同的范围(颜色条的限制)并指定颜色。

rng = range(matrixA, matrixB, matrixC)

And add this to your ggplot code:并将其添加到您的ggplot代码中:

g + scale_fill_gradient2(low="green", mid="lightblue", high="red", #colors in the scale
               midpoint=mean(rng),    #same midpoint for plots (mean of the range)
               breaks=seq(0,1,0.25), #breaks in the scale bar
               limits=c(floor(rng[1]), ceiling(rng[2])))

Example:例子:

Below is an example that helps you to get what you want:下面是一个可以帮助您获得所需内容的示例:

x <- matrix(60:85, 5)/100
y <- matrix(65:95, 5)/100
z <- matrix(50:100, 5)/100


rng = range(c((x), (y), (z)))

library(reshape)
library(ggplot2)

ggplot(data = melt(x)) + geom_tile(aes(x=X1,y=X2,fill = value)) +
  scale_fill_gradient2(low="green", mid="lightblue", high="red", #colors in the scale
                   midpoint=mean(rng),    #same midpoint for plots (mean of the range)
                   breaks=seq(0,1,0.25), #breaks in the scale bar
                   limits=c(floor(rng[1]), ceiling(rng[2]))) + #same limits for plots
                   ggtitle("X")

ggplot(data = melt(y)) + geom_tile(aes(x=X1,y=X2,fill = value)) +
  scale_fill_gradient2(low="green", mid="lightblue", high="red", 
                   midpoint=mean(rng),   
                   breaks=seq(0,1,0.25), 
                   limits=c(floor(rng[1]), ceiling(rng[2]))) +
                    ggtitle("Y")                  
  
ggplot(data = melt(z)) + geom_tile(aes(x=X1,y=X2,fill = value)) +
 scale_fill_gradient2(low="green", mid="lightblue", high="red", 
                   midpoint=mean(rng),    
                   breaks=seq(0,1,0.25), 
                   limits=c(floor(rng[1]), ceiling(rng[2]))) +
                    ggtitle("Z")   

This will give you:这会给你:

I need to draw 3 different plots setting the same scale range color.我需要绘制 3 个不同的图,设置相同的比例范围颜色。 I have 3 matrices with different range of values.我有 3 个具有不同值范围的矩阵。

For example:例如:

range(matrixA) 
# 0.60 0.85  
range(matrixB) 
# 0.65 0.95  
range(matrixA) 
# 0.5 1.0

I would like to have the same color fill for the plots.我想为绘图填充相同的颜色。 For example, for all 0.8 value in the difference plots, if in the first plot 0.8 orange, I want all 0.8 value in different graphs to be the same orange.例如,对于差异图中的所有 0.8 值,如果在第一个图中 0.8 橙色,我希望不同图中的所有 0.8 值都是相同的橙色。

My problem in this moment is:我此刻的问题是:

In the first plot, the color of the max value is red, then the value 0.85 is red.在第一个图中,最大值的颜色是红色,然后值 0.85 是红色。

In the second plot, the max value is red but in this case the max value is 0.95 and the problem arises.在第二个图中,最大值为红色,但在这种情况下,最大值为 0.95,问题出现了。

My code:我的代码:

mat.melted <- melt(matrixA)
colnames(mat.melted) <- c("p","c","v")
p <- ggplot(mat.melted, aes(x=c,y=p,fill=v) + 
         geom-tile() + 
         scale_fill_gradintn(limits = c(min(as.vector(matrixA)), max(as.vector(matrixA))), 
                             colors = c("yellow","orange","red"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM