如何在r中的热图单元格中显示数字单元格值 [英] How to show the numeric cell values in heat map cells in r

查看:885
本文介绍了如何在r中的热图单元格中显示数字单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个热图,显示驱动每个单元格中颜色的值。如果一个单元格是深蓝色的,因为它有5个观测值,我想看看那个单元格中的数字5.
$ b (目的是建立一个信用评级偏移矩阵,其中一个轴是今年的信用评级,另一个是去年的信用评级,输入是一个数据框,每一行对一家公司来说是一个观察值,公司今年的信用评级,以及去年的信用评级。在两年的时间内获得稳定的信用评级,被评为较低的评级,并获得较高的评级)。



这里是数据和代码

  require(ggplot2) 

#创建一个数据框mm,其中每行是一个公司的观察值,
#是公司今年的信用评级,以及去年的信用评级。公司ID为
#。


mm <-data.frame(
CompamyID = c(1:14),
CurrentYear = c(Aaa,Aa,B ,Baa,C,Aaa,Aa,B,Baa,C,Aa,B,Baa,C b上一年= c(Aaa,Aa,B,Baa,Aa,B,Baa,C,C,AaaAa B,Baa,C),
Count = rep(1,14)


#创建热图并显示每个单元格中观察值的数量。
#我已经使用label =#sum()来说明,但它是错误的。
$ b ggplot(data = mm,aes(x = mm $ CurrentYear,y = mm $ PreviousYear))+
geom_bin2d()+
geom_text(aes(fill = mm $ Count,label = sum(mm $ Count)))+
scale_x_discrete(limits = c(Aaa,Aa,A,Baa,Ba,B ,Ca,C))+
scale_y_discrete(限制= c(C,Ca,Caa,B,Ba,Baa Aa,Aaa))+
scale_fill_gradient2()+
主题(panel.grid.major = element_line(color =white,size = 0.5))+
主题.grid.minor = element_line(color =black,linetype =dashed,size = 0.5))+
theme(panel.background = element_rect(color =black,fill =white,size =)+
ggtitle(使用geom_bin2d()迁移矩阵)+
xlab(当前年)+
ylab(上一年)


解决方案

我会使用 stat_bin2d 所以 ggplot2 在内部计算这些计数并使它们在名称下可用。count ..

  ggplot(data = mm,aes(x = mm $ CurrentYear,y = mm $ PreviousYear))+ 
geom_bin2d()+
stat_bin2d(geom =text,aes(label = .. count ..))+
scale_x_discrete(limits = c(Aaa,Aa,A, Baa,Ba,B,Caa,Ca,C))+
scale_y_discrete(limits = c(C,Ca,Caa,B ),Ba,Baa,A,Aa,Aaa))+
scale_fill_gradient2()+
主题(panel.grid.major = element_line(color =white ,size = 0.5))+
theme(panel.grid.minor = element_line(color =black,linetype =dashed,size = 0.5))+
theme(panel.background =元素正确(color =black,fill =white,size = 1.0))+
ggtitle(使用geom_bin2d()迁移矩阵)+
xlab(当前年份)+
ylab(上一年)

我希望有帮助。


I am trying to create a heat map that shows the value that drive the colors in each cell. If a cell is dark blue because it has 5 observations, I want to see the number 5 in that cell.

(The purpose is to buid a credit ratings migration matrix where one axis is credit ratings this year, the other is credit ratings last year. The input is a dataframe where each row is one observation for one company, the company's credit rating this year, and it credit rating last year. The matrix show which companies have stable credit ratings over a two year period, which were assigned a lower rating, and which moved to a higher rating).

Here is the data and the code

require(ggplot2)

# Create a dataframe mm where each row is one observation for one company, 
# the company's credit rating this year, and it credit rating last year.  A company ID is 
# provided.  


mm<-data.frame(
    CompamyID = c(1:14),
    CurrentYear =c("Aaa","Aa","B","Baa","C","Aaa","Aa","B","Baa","C","Aa","B","Baa","C"),
    PreviousYear=c("Aaa","Aa","B","Baa","Aa","B","Baa","C","C","Aaa","Aa","B","Baa","C"),
    Count=rep(1,14)
)

# Create heatmap and show the number of observations in each cell.  
# I have used label= # sum() for illustration but it is wrong.  

ggplot(data=mm, aes(x = mm$CurrentYear, y=mm$PreviousYear)) + 
    geom_bin2d() + 
    geom_text(aes(fill = mm$Count, label = sum(mm$Count)))+
    scale_x_discrete(limits =c( "Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa", "Ca", "C")) +
    scale_y_discrete(limits=c("C","Ca","Caa","B","Ba", "Baa", "A", "Aa", "Aaa")) + 
    scale_fill_gradient2() + 
    theme(panel.grid.major = element_line( colour ="white", size = 0.5 ))+ 
    theme(panel.grid.minor = element_line( colour ="black", linetype ="dashed", size = 0.5)) +
    theme(panel.background = element_rect( colour ="black", fill ="white",size = 1.0 )) +
    ggtitle("MIGRATION MATRIX USING geom_bin2d()") +
    xlab("Current Year") +
    ylab("Previous Year")         

解决方案

I would use stat_bin2d so ggplot2 internally computes the counts and makes them available under the name ..count...

ggplot(data=mm, aes(x = mm$CurrentYear, y=mm$PreviousYear)) + 
  geom_bin2d() + 
  stat_bin2d(geom="text", aes(label=..count..))+
  scale_x_discrete(limits =c( "Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa", "Ca", "C")) +
  scale_y_discrete(limits=c("C","Ca","Caa","B","Ba", "Baa", "A", "Aa", "Aaa")) + 
  scale_fill_gradient2() + 
  theme(panel.grid.major = element_line( colour ="white", size = 0.5 ))+ 
  theme(panel.grid.minor = element_line( colour ="black", linetype ="dashed", size = 0.5)) +
  theme(panel.background = element_rect( colour ="black", fill ="white",size = 1.0 )) +
  ggtitle("MIGRATION MATRIX USING geom_bin2d()") +
  xlab("Current Year") +
  ylab("Previous Year")

I hope that helps.

这篇关于如何在r中的热图单元格中显示数字单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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