Home > Net >  ggplot Correlational Heatmap With Sums Instead of Correlations
ggplot Correlational Heatmap With Sums Instead of Correlations

Time:11-17

I have a dataset of legislation that was passed in state houses that is coded into different types of bills. The bills overlap (e.g., a bill could cover topics A and B, or B and C, or just A).

I am trying to create a visualization similar to the correlational heatmap below, but with the total sums of values rather than the mere correlations (e.g., there would be 0s, 1s, 2s, 3s, et cetera, for where bills overlap). I think the correlational plot method would look best to capture this information, but I have not seen any that post the sums instead of the correlational values. Does anyone have any ideas about how to approach this issue? enter image description here

Thanks as always for the help--I greatly appreciate this community!

CodePudding user response:

If I've understood your question, geom_tile should do what you need:

library(ggplot2)

df <- tibble::tribble(
  ~bill_1, ~bill_2, ~overlap,
      "A",     "A",      10L,
      "A",     "B",       4L,
      "A",     "C",       3L,
      "A",     "D",       2L,
      "A",     "E",       0L,
      "B",     "B",       6L,
      "B",     "C",       5L,
      "B",     "D",       3L,
      "B",     "E",       1L,
      "C",     "C",       7L,
      "C",     "D",       5L,
      "C",     "E",       2L,
      "D",     "D",       5L,
      "D",     "E",       1L,
      "E",     "E",       3L
  )

ggplot(df, aes(x=bill_2, y=bill_1))  
  geom_tile(aes(fill = overlap))  
  geom_text(aes(label=overlap), colour = "white", check_overlap = TRUE)  

enter image description here

  • Related