Plots a dispersion or "x-ray" plot of selected word pattern(s) across one or more texts. The format of the plot depends on the number of kwic class objects passed: if there is only one document, keywords are plotted one below the other. If there are multiple documents the documents are plotted one below the other, with keywords shown side-by-side. Given that this returns a ggplot2 object, you can modify the plot by adding ggplot2 layers (see example).

textplot_xray(..., scale = c("absolute", "relative"), sort = FALSE)



any number of kwic class objects


whether to scale the token index axis by absolute position of the token in the document or by relative position. Defaults are absolute for single document and relative for multiple documents.


whether to sort the rows of a multiple document plot by document name


a ggplot2 object


data_corpus_inauguralPost70 <- corpus_subset(data_corpus_inaugural, Year > 1970)
# compare multiple documents
textplot_xray(kwic(data_corpus_inauguralPost70, "american"))
textplot_xray(kwic(data_corpus_inauguralPost70, "american"), scale = "absolute")
# compare multiple terms across multiple documents
textplot_xray(kwic(data_corpus_inauguralPost70, "america*"),
              kwic(data_corpus_inauguralPost70, "people"))

# how to modify the ggplot with different options
g <- textplot_xray(kwic(data_corpus_inauguralPost70, "american"),
                   kwic(data_corpus_inauguralPost70, "people"))
g + aes(color = keyword) + scale_color_manual(values = c('red', 'blue'))

# adjust the names of the document names
docnames(data_corpus_inauguralPost70) <- apply(docvars(data_corpus_inauguralPost70,
                                                       c("Year", "President")),
                                              1, paste, collapse = ", ")
textplot_xray(kwic(data_corpus_inauguralPost70, "america*"),
              kwic(data_corpus_inauguralPost70, "people"))
# }