Learn R Programming

PsychWordVec (version 2023.9)

plot_wordvec: Visualize word vectors.

Description

Visualize word vectors.

Usage

plot_wordvec(x, dims = NULL, step = 0.05, border = "white")

Value

A ggplot object.

Arguments

x

Can be:

  • a data.table returned by get_wordvec

  • a wordvec (data.table) or embed (matrix) loaded by data_wordvec_load

dims

Dimensions to be plotted (e.g., 1:100). Defaults to NULL (plot all dimensions).

step

Step for value breaks. Defaults to 0.05.

border

Color of tile border. Defaults to "white". To remove the border color, set border=NA.

Download

Download pre-trained word vectors data (.RData): https://psychbruce.github.io/WordVector_RData.pdf

See Also

get_wordvec

plot_similarity

plot_wordvec_tSNE

Examples

Run this code
d = as_embed(demodata, normalize=TRUE)

plot_wordvec(d[1:10])

dt = get_wordvec(d, cc("king, queen, man, woman"))
dt[, QUEEN := king - man + woman]
dt[, QUEEN := QUEEN / sqrt(sum(QUEEN^2))]  # normalize
names(dt)[5] = "king - man + woman"
plot_wordvec(dt[, c(1,3,4,5,2)], dims=1:50)

dt = get_wordvec(d, cc("boy, girl, he, she"))
dt[, GIRL := boy - he + she]
dt[, GIRL := GIRL / sqrt(sum(GIRL^2))]  # normalize
names(dt)[5] = "boy - he + she"
plot_wordvec(dt[, c(1,3,4,5,2)], dims=1:50)

dt = get_wordvec(d, cc("
  male, man, boy, he, his,
  female, woman, girl, she, her"))

p = plot_wordvec(dt, dims=1:100)

# if you want to change something:
p + theme(legend.key.height=unit(0.1, "npc"))

# or to save the plot:
ggsave(p, filename="wordvecs.png",
       width=8, height=5, dpi=500)
unlink("wordvecs.png")  # delete file for code check

Run the code above in your browser using DataLab