Skip to content

Instantly share code, notes, and snippets.

@samclifford
Created April 18, 2017 05:26
Show Gist options
  • Select an option

  • Save samclifford/8f03bc53e852a61d4738f09e9c90adf8 to your computer and use it in GitHub Desktop.

Select an option

Save samclifford/8f03bc53e852a61d4738f09e9c90adf8 to your computer and use it in GitHub Desktop.
Plotting an adjacency matrix
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