3

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 Answers1

5

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.

ilir
  • 3,236
  • 15
  • 23
  • If you appreciate it, don't forget to upvote and accept the answer. – juba Oct 03 '14 at 13:13
  • Is there anything similar than `stat_bin2d` for R 3.1.1? I would like to get its independence because I get the warning `> install.packages("stat_bin2d") Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) Warning message: package ‘stat_bin2d’ is not available (for R version 3.3.1) ` – Léo Léopold Hertz 준영 Nov 01 '16 at 16:15