具有离散值的热图/图像 [英] Heat map/image with discrete values

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

问题描述

我有一个数据框,其中包含日期和该日期的状态.状态范围是-1到2.我想创建一个热图(按星期几和一年中的星期几分组).

I have a dataframe, it contains dates and status at this date. Status ranges from -1 to 2. I would like to create a heat map (grouped by days of the week and number of the week in the year), which is not a problem.

我的问题是我想要以下颜色映射:

My problem is i want the following color mapping:

-1 => gray
0 => white
1 => blue
2 => green

有什么想法吗?.谢谢!

Any ideas?. Thank you!

按照我下面将要执行的方式请求的示例代码,它对我不起作用:

As requested a sample code below the way I would do it and it doesnt work for me:

breaks <- seq(-2,2,1)
pal <- c("gray","white","green","blue")
data <- sample(-1:2, 5*6, replace=T)
m <- matrix(unlist(data),ncol=6)
m
image(m,
      col = c("gray","white","green","blue"),
      breaks = c(-2,-1,0,1,2)
      )

推荐答案

以下内容似乎可以满足您的要求:

The following seems to do what you want:

set.seed(14)
a<-matrix(floor(runif(21)*4)-1,nrow=7)
col<-c("grey","white","blue","green")
image(1:8,1:4,a,col=col)

对于gplot版本:

#color scale
col<-c("red","orange","grey","lightblue","darkblue")
#creating the dataframe for 3 example weeks
(d<-t(matrix(c(1,1,2,3,2,3,4,
    2,1,2,2,3,4,4,
    5,3,2,4,5,4,5),nrow=3,byrow=TRUE)))
df<-data.frame(day=rep(1:7,3),week=rep(1:3,each=7),werte=as.numeric(d))
#the ggplot
p<-ggplot(df,aes(x=day,y=max(df$week)+1-week))+
    #tile layer
    geom_tile(aes(fill=factor(werte))) +
    #setting the color
    scale_fill_manual(
        values=col,
        breaks=c("1","2","3","4","5"),
        labels=c("one","two","three","four","five"))

p

这篇关于具有离散值的热图/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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