Created
December 9, 2016 20:11
-
-
Save bmschmidt/2d69690604466b6daa83bc952dc593a7 to your computer and use it in GitHub Desktop.
A slopegraph in R for use with with the wordVectors package
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(ggplot2) | |
library(wordVectors) | |
slopegraph = function( | |
set1 = "RMP" | |
, | |
set2 = "genderless_RMP" | |
, | |
word = "she" | |
, | |
length=10 | |
) { | |
s1 = get(set1) | |
s2 = get(set2) | |
all_words = c(names(nearest_to(s1,s1[[word]],length)),names(nearest_to(s2,s2[[word]],length))) | |
all_words = all_words[all_words!=word] # Who cares, this will always be 1. | |
p01 = as.vector(cosineSimilarity(s1[[word]],s1)) | |
p02 = as.vector(cosineSimilarity(s2[[word]],s2)) | |
p1 = p01[match(all_words,rownames(s1))] | |
p1Ranks = rank(-p01)[match(all_words,rownames(s1))] | |
p2 = p02[match(all_words,rownames(s2))] | |
p2Ranks = rank(-p02)[match(all_words,rownames(s2))] | |
plottable = data.frame(word = as.vector(all_words),set1 = as.vector(p1),set2 = as.vector(p2),rank1=p1Ranks,rank2=p2Ranks) | |
library(ggplot2) | |
ggplot(plottable) + geom_text(aes(y=set1,label=paste0(word," (",rank1,")")),hjust=1,x=1,alpha=.5) + | |
geom_text(aes(y=set2,label=paste0(word," (",rank2,")")),hjust=0,x=2,alpha=.5) + | |
geom_segment(aes(x=1,xend=2,y=set1,yend=set2)) + scale_x_continuous(limits=c(0,3)) + | |
scale_y_continuous(paste0("Cosine similarity to ",word))+ | |
theme_bw() + | |
labs (title=paste0("Similarity to '", word,"' in the\nvectorspaces ", set1, " (left) and\n", set2, " (right)")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment