Skip to content

Instantly share code, notes, and snippets.

@cigrainger
Last active March 24, 2016 18:59
Show Gist options
  • Save cigrainger/73f00996e93abb4a3e2a to your computer and use it in GitHub Desktop.
Save cigrainger/73f00996e93abb4a3e2a to your computer and use it in GitHub Desktop.
library(quanteda)
library(dplyr)
library(tidyr)
library(networkD3)
load(url("http://www.kenbenoit.net/files/presDebateCorpus2016seg.RData"))
candidates <- subset(presDebateCorpus2016seg, speakertype == 'candidate')
dfm <- dfm(candidates, groups = c("tag"), ngrams = 1:3,
ignoredFeatures = c('people','go','going','will','know','think',
'country','get','applause','want','need',
'can','say','president','one','well','make',
'us','said',stopwords("english")),
stem = TRUE)
dist <- as.matrix(similarity(dfm))
cd <- row.names(dist)
candidate_list <- c("RUBIO","CLINTON","SANDERS","TRUMP","CRUZ","BUSH","O'MALLEY")
dist <- as.data.frame(dist) %>%
mutate(cd = cd) %>%
gather(key = to, value = dist, -cd) %>%
rename(from = cd) %>%
mutate(dist = 1-dist) %>%
filter(dist !=0) %>%
filter(from %in% candidate_list,
to %in% candidate_list)
nodes <- unique(c(dist$from,dist$to))
nodes <- data.frame(NodeID = nodes, stringsAsFactors = F)
party <- data.frame(candidates$documents$party,candidates$documents$tag) %>%
unique() %>%
rename(group = candidates.documents.party,
NodeID = candidates.documents.tag)
nodes <- inner_join(nodes,party)
from <- data.frame(from = nodes$NodeID,
from_id = seq(0,nrow(nodes)-1))
to <- data.frame(to = nodes$NodeID,
to_id = seq(0,nrow(nodes)-1))
dist <- dist %>%
inner_join(from) %>%
inner_join(to) %>%
select(-from,-to) %>%
rename(from=from_id,to=to_id)
nodes$group <- as.character(nodes$group)
nodes$group[nodes$group == 'republican'] <- 'Republican'
nodes$group[nodes$group == 'democratic'] <- 'Democrat'
forceNetwork(Links = dist, Nodes = nodes,
Source = "from", Target = "to",
Value = "dist", NodeID = "NodeID",
Group = "group", opacity = 0.8,
colourScale = JS("d3.scale.category10()"),
zoom = TRUE, legend = TRUE,
linkDistance = JS("function(d){return d.value * 200}"),
linkWidth = 0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment