ggplot2带有颜色范围值的热图 [英] ggplot2 heatmap with colors for ranged values

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

问题描述

我想在ggplot2中制作一个热图。我的玩具数据和代码是:

  set.seed(12345)
dat < -
data
,Col = rep(x = LETTERS [1:10],each = 5)
Row = rep(x = LETTERS [1:5],times = 10) ,Y = rnorm(n = 50,mean = 0,sd = 1)

library(ggplot2)
p < - ggplot(data = dat,aes(x = Row,y )=
geom_tile(aes(fill = Y),color =white)+
scale_fill_gradient(low =white,high =steelblue)
p

我想要这样的范围值的颜色方案:

  -3 <= Y < -2 --->深蓝色
-2 <= Y < -1 --->蓝色
-1 <= Y < 0 --->淡蓝色
0 <= Y < 1 --->浅绿色
1 <= Y < 2 --->绿色
2 <= Y <= 3 --->深绿色

任何帮助将不胜感激。谢谢

解决方案

你有几种选择,但这里有一个作为起点。
$ b

首先,使用 cut Y 中创建一个适当范围的因子:
$ b $ pre $ dat $ Y1 < - cut(dat $ Y,breaks = c(-Inf,-3:3,Inf) ,right = FALSE)然后使用 RColorBrewer 中的调色板进行绘图: >

  ggplot(data = dat,aes(x = Row,y = Col))+ 
geom_tile(aes(fill = Y1),color =white)+
scale_fill_brewer(palette =PRGn)



这种配色方案在低端,它的颜色比蓝色更紫,但它是我在啤酒调色板中找到的最接近的。



如果你想建立你自己的,你可以简单地使用 scale_fill_manual 并指定y我们期望的参数的颜色向量。


I want to make a heatmap in ggplot2. My toy data and code is:

set.seed(12345)
dat <- 
  data.frame(
      Row = rep(x = LETTERS[1:5], times = 10)
    , Col = rep(x = LETTERS[1:10], each = 5)
    , Y = rnorm(n = 50, mean = 0, sd = 1)
    )
library(ggplot2)
p <- ggplot(data =  dat, aes(x = Row, y = Col)) + 
      geom_tile(aes(fill = Y), colour = "white") +
      scale_fill_gradient(low = "white", high = "steelblue")
p

I want to have color scheme for ranged values like this:

-3 <= Y < -2  ---> Dark Blue
-2 <= Y < -1  ---> Blue
-1 <= Y <  0  ---> Light Blue
 0 <= Y <  1  ---> Light Green
 1 <= Y <  2  ---> Green
 2 <= Y <= 3  ---> Dark Green

Any help will be highly appreciated. Thanks

解决方案

You have several options for something like this, but here is one as a starting point.

First, use cut to create a factor from Y with the appropriate ranges:

dat$Y1 <- cut(dat$Y,breaks = c(-Inf,-3:3,Inf),right = FALSE)

Then plot using a palette from RColorBrewer:

ggplot(data =  dat, aes(x = Row, y = Col)) + 
      geom_tile(aes(fill = Y1), colour = "white") +
      scale_fill_brewer(palette = "PRGn")

This color scheme is more purple than blue on the low end, but it's the closest I could find among the brewer palette's.

If you wanted to build your own, you could simply use scale_fill_manual and specify your desired vector of colors for the values argument.

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

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