How to show the numeric cell values in heat map cells in r

谁都会走 提交于 2019-12-10 20:35:07

问题


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")         

回答1:


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.



来源:https://stackoverflow.com/questions/26177134/how-to-show-the-numeric-cell-values-in-heat-map-cells-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!