TextPlot: R Library for Visualizing Text Data | Towards Data Science

Data visualization is an essential task in data science. We can gain insights from the data visualization so it can support our decision for our problems.
Text data is one of the most analyzed data by many people. It is also one of the most complex data because we have to spend a lot of time preprocessing the data, ranging from tokenizing the text, deleting terms that are not meaningful, creating a document term matrix, etc.
Visualizing data like text sometimes can have the same complexities as preprocessing it. Thankfully, there’s a library that can help us to visualize text data. It’s called TextPlot.
TextPlot is a library that is implemented with the R programming language. TextPlot can visualize many things, for example, word frequencies chart, word correlation graph, dependency parsing, and many more.
In this article, I will show you how to use the TextPlot library for visualizing text data using R. Without further, let’s get started!
Before we play with the library, we need to install the libraries and load them into our environment. Here is the script for doing that,
install.packages("textplot")install.packages('udpipe')install.packages('igraph')install.packages('ggraph')install.packages('concaveman')install.packages('BTM')install.packages('glasso')install.packages('qgraph')install.packages('graph')library(ggraph)library(concaveman)library(ggplot2)library(BTM)library(textplot)library(udpipe)library(igraph)library(graph)library(Rgraphviz)
The first visualization that you can do with this library is the word frequencies bar chart. For the input, it takes a table that has words and their frequencies. Let’s see how does it look by running this code first,
data(brussels_listings, package = 'udpipe')x <- table(brussels_listings$neighbourhood)x <- sort(x)x
Here is the preview of the input looks like,
To create word frequencies bar chart, you need to run the textplot_bar function from textplot library. Here is the command look like,
textplot_bar(x, panel = "Locations", col.panel = "darkgrey", xlab = "Listings", cextext = 0.75, addpct = TRUE, cexpct = 0.5)
Here is the chart looks like,
As you can see above, the x-axis represents the number of words, and the y axis represents the word itself. Also, each word has information like the amount and the percentage of the word.
Another visualization that you can create is the word similarity chart. This chart will visualize the similarity between words in a graph representation.
Document Term Matrix (DTM) will be used as the input. DTM is a text representation where each row represents a document, each column represents words, and each cell represents the frequency of words in a document.
To create a DTM, you can use command from a text plot like this,
data(brussels_reviews_anno, package = 'udpipe')x <- subset(brussels_reviews_anno, xpos %in% "NN" & language %in% "nl" & !is.na(lemma))x <- document_term_frequencies(x, document = "doc_id", term = "lemma")dtm <- document_term_matrix(x)dtm <- dtm_remove_lowfreq(dtm, maxterms = 60)dtm
Here is how the matrix look like,
After you create the DTM, the next step is to create a correlation matrix by calculating the correlation of each word. To do that, we can use the dtm_cor function where it takes the DTM as the parameter. Here is the command look like,
m <- dtm_cor(dtm)
Here is the preview of the correlation matrix look like,
Now you can create the word correlation graph chart. To do that, we can use the textplot_correlation_glasso function to generate the chart.
Besides giving the input, we also set the exclude_zero parameter to true, so it doesn’t show the words that have zero correlation. Here is what the function looks like,
textplot_correlation_glasso(m, exclude_zero = TRUE)
Here is the result,
As you can see above, as the line gets thicker, the correlation is also getting bigger. For example, you can see that word ‘badkamer’, ‘slaapkamer’, and ‘woonkamer’ relates stronger than any words on the graph. Those words are from the dutch language, and they have the same word ‘kamer’ that occur on them. It means room.
Another visualization that I want to show you is dependency parsing. Dependency parsing is a way to see which word is dependent on another and how do words relate to each other.
Let’s take this sentence as input.
sentence <- “UDPipe provides tokenization, tagging, lemmatization and dependency parsing of raw text”
To retrieve the dependency parsing, we need to tokenize the sentence and assign the part-of-speech (POS) tag to each word. Thankfully, this process is already done by the udpipe function from the udpipe library. The command looks like this,
x <- udpipe(sentence, "english")
After that, we will generate the dependency parsing with the textplot_dependencyparser function from the textplot library. The command looks like this,
textplot_dependencyparser(x)
Here is how the visualization look like,
As you can see above, the chart shows the POS tag below the word and the arrow that draws the relationship between words.
The last visualization that I want to show you is the word cooccurrence graph. This graph visualizes collections of word pair and how frequent these word pairs occur.
We can use a function called textplot_cooccurrence to visualize this chart. Now let’s take the data first. We will use the same data that we use above.
With that data, we create a cooccurrence data frame where each row contains the word pair and how many they occur. That cooccurrence data frame will be used as the input for the textplot_cooccurrence function.
The code for the data looks like this,
data(brussels_reviews_anno, package = 'udpipe')x <- subset(brussels_reviews_anno, xpos %in% "JJ" & language %in% "fr")x <- cooccurrence(x, group = "doc_id", term = "lemma")
Here is the data look like,
After we get the correct input, now we can create the chart. We will take only the 25 most occur word pairs. Here is the command to do that,
textplot_cooccurrence(x, top_n = 25, subtitle = "showing only top 25")
Here is the visualization,
As you can see, the word pair of ‘agreable’ and ‘bon’ is the most occur on the data.
Congratulations! Now you have learned how to use text plot in R. I hope you can use this library on your data science pipeline for solving your data visualization, especially on text data.
If you want to see more of my writing, you can follow me on medium. Also, if you have any questions or to have a conversation with me, you can connect with me on LinkedIn.
Thank you for reading my article!