ggplot2热图,图形之间具有固定比例的颜色条 [英] ggplot2 heatmap with fixed scale colorbar between graphs

查看:185
本文介绍了ggplot2热图,图形之间具有固定比例的颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制3个设置相同刻度范围颜色的不同图. 我有3个值范围不同的矩阵.

I need to draw 3 different plots setting the same scale range color. I have 3 matrices with different range of values.

例如:

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

我想为这些图填充相同的颜色.例如,对于差异图中的所有0.8值,如果在第一个图中为0.8橙色,我希望不同图中的所有0.8值都为相同的橙色.

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.85为红色.

In the first plot, the color of the max value is red, then the value 0.85 is red.

在第二个图中,最大值为红色,但在这种情况下,最大值为0.95,因此出现了问题.

In the second plot, the max value is red but in this case the max value is 0.95 and the problem arises.

我的代码:

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)

并将其添加到您的ggplot代码中:

And add this to your ggplot code:

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])))

示例:

下面是一个示例,可帮助您获得所需的东西:

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")   

这将为您提供:

这篇关于ggplot2热图,图形之间具有固定比例的颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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