How to show the numeric cell values in heat map ce

2019-07-04 02:16发布

问题:

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.