2颜色热图在R中,中间颜色锚定到一个特定的值 [英] 2 color heatmap in R with middle color anchored to a specific value
本文介绍了2颜色热图在R中,中间颜色锚定到一个特定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个热图贴图是在Excel中生成的,但是当我想学习RI想知道如何使用R创建一个热图。
现在,此代码正常工作,但不工作因为它应该...
(p < - ggplot(melt,heat),aes(Var2,Var1) )
+ geom_tile(aes(fill = value),color =white)
+ scale_fill_gradient(low =red,high =green))
我想有2个渐变,绿色(最高价值)到白色(数字1000)到红色(最低价值)。是否可能在R?
热数据集:
heat [0:10,0:10]
[,1] [,2] [,3] [,4] [,5] [10]
[1] 1000.000 0.000 0.0000 0.0000 757.0317 709.3896 843.7676 932.2801 0.0000 1016.7203
[2] 1087.658 1000.000 0.0000 0.0000 935.5829 854.5110 889.5042 1091.4610 929.1611 0.0000
[3] 1181.599 1361.953 1000.0000 0.0000 0.0000 1102.1590 1147.1300 984.1374 969.0718 1058.3456
[4] 1319.012 1405.954 1187.5215 1000.0000 0.0000 1093.8854 1195.7298 1077.0797 1119.4640 1159.5207
[5] 0.000 0.000 909.1927 817.5097 1000.0000 0.0000 1101.2891 1064.6516 1037.1623 990.3974
[6 ,] 0.000 0.000 0.0000 0.0000 887.7498 1000.0000 1015.9835 1062.1668 1105.2163 983.2319
[7] 0.000 0.000 0.0000 0.0000 0.0000 0.0000 1000.0000 911.0699 1026.1989 951.3572
[8] 0.000 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 834.8725 927.6802
[9] 1261.824 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 795.6285
[10] 0.000 1121.210 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000
解决方案
您可以使用<?code> scale_fill_gradientn
获取您的价值:
图书馆(量表)
heat.dat< 0:10,0:10]
dat< - expand.grid(var1 = 1:10,var2 = 1:10)
dat $ value< - melt(heat.dat)$ value
ggplot(DAT,AES(X = VAR1,Y = VAR2))+
geom_tile(AES(填写=值),颜色='白色')+
scale_fill_gradientn(颜色= C( red,white,green),
values = rescale(c(min(dat $ value),1000,max(dat $ value)))
/ p>
使用Lattice:
与(dat,
levelplot (...){
arg< - list(...)
面板(...)(round(value,0)〜y * x,
dat,
panel = function .levelplot(...)
panel.text(arg $ x,arg $ y,arg $ z)},
scales = list(y = list(at = y,labels = y)
x = list(at = y,labels = y)),
col.regions = colorRampPalette(c(red,white,green),interpolate ='spline'))
)
This heatmap image was generated in Excel but as I'm trying to learn R I would like to know how to make a heatmap like that with R.
Right now, this code is working but not as it was supposed to...
(p <- ggplot(melt(heat), aes(Var2, Var1))
+ geom_tile(aes(fill = value), colour = "white")
+ scale_fill_gradient(low = "red", high = "green"))
I would like to have 2 gradients, green(highest value) to white (the number 1000) to red(lowest value). Is it possible in R?
heat dataset:
> heat[0:10,0:10]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1000.000 0.000 0.0000 0.0000 757.0317 709.3896 843.7676 932.2801 0.0000 1016.7203
[2,] 1087.658 1000.000 0.0000 0.0000 935.5829 854.5110 889.5042 1091.4610 929.1611 0.0000
[3,] 1181.599 1361.953 1000.0000 0.0000 0.0000 1102.1590 1147.1300 984.1374 969.0718 1058.3456
[4,] 1319.012 1405.954 1187.5215 1000.0000 0.0000 1093.8854 1195.7298 1077.0797 1119.4640 1159.5207
[5,] 0.000 0.000 909.1927 817.5097 1000.0000 0.0000 1101.2891 1064.6516 1037.1623 990.3974
[6,] 0.000 0.000 0.0000 0.0000 887.7498 1000.0000 1015.9835 1062.1668 1105.2163 983.2319
[7,] 0.000 0.000 0.0000 0.0000 0.0000 0.0000 1000.0000 911.0699 1026.1989 951.3572
[8,] 0.000 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 834.8725 927.6802
[9,] 1261.824 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 795.6285
[10,] 0.000 1121.210 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000
解决方案
You can use ?scale_fill_gradientn
gving you values:
library(scales)
heat.dat <- heat[0:10,0:10]
dat <- expand.grid(var1=1:10, var2=1:10)
dat$value <- melt(heat.dat)$value
ggplot(dat, aes(x=var1,y=var2))+
geom_tile(aes(fill = value),colour='white')+
scale_fill_gradientn(colours=c("red","white","green"),
values = rescale(c(min(dat$value), 1000, max(dat$value)))
Using Lattice:
with(dat,
levelplot(round(value,0)~y*x,
dat,
panel=function(...) {
arg <- list(...)
panel.levelplot(...)
panel.text(arg$x, arg$y,arg$z)},
scales = list(y = list(at=y,labels=y),
x = list(at=y,labels=y)),
col.regions = colorRampPalette(c("red", "white", "green"),interpolate ='spline'))
)
这篇关于2颜色热图在R中,中间颜色锚定到一个特定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文