R带有可控制的颜色和阈值的热图? [英] R Heatmap with controlled colors and thresholds?

查看:72
本文介绍了R带有可控制的颜色和阈值的热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Test1  Test2  Test3 Test4  Test5
0.048  0.552  0.544 1.0000 0.604
0.518  0.914  0.948 0.0520 0.596
0.868  0.944  0.934 0.3720 0.340
0.934  0.974  0.896 0.1530 0.090
0.792  0.464  0.096 0.7050 0.362
0.001  0.868  0.050 0.3690 0.380
0.814  0.286  0.162 0.0040 0.092
0.146  0.966  0.044 0.4660 0.726
0.970  0.862  0.001 0.4420 0.020
0.490  0.824  0.634 0.5720 0.018
0.378  0.974  0.002 0.0005 0.004
0.878  0.594  0.532 0.0420 0.366
1.000  1.000  1.000 0.3550 1.000
1.000  1.000  1.000 0.3170 1.000
1.000  1.000  1.000 0.3900 1.000
0.856  0.976  0.218 0.0005 0.001
1.000  1.000  1.000 0.4590 1.000
1.000  1.000  1.000 0.5770 1.000
0.640  0.942  0.766 0.0005 0.320
1.000  1.000  1.000 0.0830 1.000
0.260  0.968  0.032 0.0480 0.300
0.150  0.480  0.108 0.0005 0.008
0.686  0.150  0.400 0.0200 0.060
0.002  0.346  0.004 0.0005 0.098

我想为我的值制作一个24 x 5的热图.我想根据自己的阈值设置颜色.例如,我希望所有的1.000值都为黑色.我希望0.500-0.999之间的所有值都为浅蓝色.我希望所有值0.300-0.499都略深一些.所有值0.15-0.299略深.所有值0.10-0.149略深,所有值0.5-0.099略深,所有值0.025-0.049略深,所有值0-0.024最暗.我尝试使用ggplot和heatmap(),但无法弄清楚用户设置的颜色.谢谢!

I would like to make a 24 x 5 Heatmap of my values. I want to set the colors based on my own thresholds. For example, I want all 1.000 values to be black. I would like all values between 0.500-0.999 to be very light blue. I would like all values 0.300-0.499 to be slightly darker blue. All values 0.15-0.299 slightly darker. All values 0.10-0.149 slightly darker, all values 0.5-0.099 slightly darker, all values 0.025-0.049 slightly darker, and all values 0-0.024 the darkest. I have tried using ggplot and heatmap() but I can't figure out the user set colors. Thanks!

推荐答案

ggplot中的scale_color_gradient2()函数应该可以正常工作.该部分功能的语法为

The scale_color_gradient2() function from ggplot should work just fine for this. The syntax for this portion of the function would be

p+scale_color_gradient(low='black',high='dodgerblue',mid='blue',midpoint=0.500,
    limits=c(0,1))

其中p是ggplot()+ geom_tile()[或您正在使用的任何热图函数]的图

where p is the plot from your ggplot()+geom_tile() [or whatever heatmap function you're using]

这里的假设是,您不介意从黑色到蓝色的流体标度,因为连续值将沿该滑动标度变化.如果要首先创建一个虚拟变量,该虚拟变量根据值将目标变量分配给组,则可以将scale_color_manual()函数与values = c(要在此处使用的颜色 )和breaks = c(您创建的垃圾箱的值进入此处).像这样[简化而不是您概述的存储桶总数]-

The assumption here is that you don't mind a fluid scale from black to dodger blue, as continuous values will change along that sliding scale. If you want to first create a dummy variable that assigns your target variable to a group based on value then you can use the scale_color_manual() function with values=c(colors you want go here) and breaks=c(the values of the bins you created go here). Something like this [simplified and not with the total number of buckets you've outlined] --

p<-ggplot(dataset,aes(x=x,y=y,color=binned.var)+geom_tile()+
     scale_color_manual(values=c("darkblue","blue","dodgerblue","black"),
                        breaks=c("bin1","bin2","bin3","bin4"),
                        labels=c("0-0.024","0.025-0.049","0.05-0.099","0.10-0.149"))

希望有帮助.

这篇关于R带有可控制的颜色和阈值的热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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