简体   繁体   中英

ggplot Correlational Heatmap With Sums Instead of Correlations

I have a dataset of legislation that was passed in state houses that is coded into different types of bills. The bills overlap (eg, 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 (eg, 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? 在此处输入图像描述

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

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)  

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM