R Tutorial : Displaying results with ggplot

Learn to visualize LDA topic models in R using ggplot2. Extract beta and gamma outputs with the tidy() function, then build column charts with position = "dodge" for side-by-side comparison and rotated axis labels to prevent overlap.
Chapters
In this chapter we will review how to display results of topic modeling using ggplot. As we work with LDA topic modeling, we will be interested in displaying two kinds of data. First, there are word counts in tokens. We obtain them before we fit a topic model. Then there are probabilities of topics in documents and words in topics. These are obtained after fitting a topic model. Fortunately, ggplot can do it all. Ggplot requires the data to be in tidy format. Fortunately, word counts are already in tidy format and LDA results can be converted into a tidy format using function tidy from package tidytext. When we fit a topic model, we call function lda and it returns an object among many things. This object contains two matrices, beta and gamma. Beta contains logarithms of probabilities of words belonging to topics and gamma probabilities of documents belonging to topics. Notice how in the output the dimension of beta is 2 by 342 topics and 34 words. The dimension of gamma is 5 by 25 documents and 2 topics. Function tidy takes an LDAMODEL object and returns a tidy table with a specified matrix. The geometry column layer in ggplot will produce a column chart. By default the columns will be stacked in the call to ggplot. The aesthetic specifies that values for axis x will come from column document. For axis y from column gamma dodged or side by side columns are better for telling which column is taller. To make a chart with dodged columns, we need to add position dodge argument to the col of geometry column. The example shows probabilities of words. The data comes from matrix beta contained in LDA model. The word is contained in column term. When the number of labels on x axis gets large, the labels will overlap and obscure each other. The overlap can be reduced if we rotate the labels for instance by 90 degrees. This can be achieved using the specification for axis text X element in the call to theme. Notice that in this script in this slide we first convert topic into effective variable and then use it to guide the color fill. Let's do a few examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







