R 为ggplot2中的颜色栏(图例)指定相同的限制
我希望多个绘图共享相同的颜色比例。一个绘图中的给定值应与第二个绘图中的颜色相同。如何使用R 为ggplot2中的颜色栏(图例)指定相同的限制,r,plot,ggplot2,heatmap,R,Plot,Ggplot2,Heatmap,我希望多个绘图共享相同的颜色比例。一个绘图中的给定值应与第二个绘图中的颜色相同。如何使用ggplot2强制执行此操作 以下是两个不共享色阶的绘图示例,但应: x <- matrix(1:16, 4) y <- matrix(1:16-5, 4) library(reshape) ggplot(data = melt(x)) + geom_tile(aes(x=X1,y=X2,fill = value)) ggplot(data = melt(y)) + geom_tile(aes(x
ggplot2
强制执行此操作
以下是两个不共享色阶的绘图示例,但应:
x <- matrix(1:16, 4)
y <- matrix(1:16-5, 4)
library(reshape)
ggplot(data = melt(x)) + geom_tile(aes(x=X1,y=X2,fill = value))
ggplot(data = melt(y)) + geom_tile(aes(x=X1,y=X2,fill = value))
x您需要设置缩放栏的限制,使其具有特定的颜色,并将两个绘图的平均值(中间的值)定义为相同的值
rng = range(c((x), (y))) #a range to have the same min and max for both plots
ggplot(data = melt(x)) + geom_tile(aes(x=X1,y=X2,fill = value)) +
scale_fill_gradient2(low="blue", mid="cyan", high="purple", #colors in the scale
midpoint=mean(rng), #same midpoint for plots (mean of the range)
breaks=seq(-100,100,4), #breaks in the scale bar
limits=c(floor(rng[1]), ceiling(rng[2]))) #same limits for plots
ggplot(data = melt(y)) + geom_tile(aes(x=X1,y=X2,fill = value)) +
scale_fill_gradient2(low="blue", mid="cyan", high="purple",
midpoint=mean(rng),
breaks=seq(-100,100,4),
limits=c(floor(rng[1]), ceiling(rng[2])))
这是输出: