Created
April 18, 2017 05:26
-
-
Save samclifford/8f03bc53e852a61d4738f09e9c90adf8 to your computer and use it in GitHub Desktop.
Plotting an adjacency matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(igraph) | |
| library(tidyverse) | |
| library(magrittr) | |
| dat <- data.frame(A = c(1,1,1,2,3), | |
| B = c(2,3,4,5,7)) | |
| Adj <- dat %>% | |
| as.matrix %>% | |
| graph.edgelist(directed=FALSE) %>% | |
| get.adjacency %>% | |
| as.matrix %>% | |
| multiply_by(., -1) | |
| diag(Adj) <- -rowSums(Adj) | |
| Adj %>% | |
| data.frame(., X=1:nrow(.)) %>% | |
| gather(Y, value, -X) %>% | |
| mutate(Y = parse_number(Y)) %>% | |
| ggplot(data=., aes(x=X, y=Y)) + | |
| geom_tile(aes(fill=value)) + | |
| theme_bw() + | |
| scale_fill_gradient2(low="grey20", | |
| mid ="white", | |
| high="red", | |
| midpoint = 0, | |
| name="") + | |
| coord_equal() + | |
| theme(axis.title = element_blank()) + | |
| ggtitle("Adjacency matrix") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment