2

Following the example of plotting a term-document matrix below,

library("tm")

  data("crude")

  tdm <- TermDocumentMatrix(crude, control = list(removePunctuation = TRUE,
                                                  removeNumbers = TRUE,
                                                  stopwords = TRUE))

  plot(tdm, terms = findFreqTerms(tdm, lowfreq = 6)[1:25], corThreshold = 0.5)

Is there a way to colorize the nodes based on how many vertices they have? Is there an example of making the nodes with more vertices larger or something to that effect as well?

1 Answer 1

5

I appears that the nodes that end up being plotted are of the class AgNode. The properties that you can set of the AgNode are listed on the ?AgNode help page. Once you know what properties you would like to set, you can pass a list to a nodeAttrs parameter to your plotting command. (EDIT: actually a better list is probably the node attributes description in the Rgraphviz documentation)

The nodeAttrs parameter take a list where each named element of that list corresponds to one of the properties of AgNode. At each position, you store a named vector where the name of the vector corresponds to the node name (ie the word in your term matrix) and the value represents the value for that attribute. For example

list(
    color=c(futures="blue", demand="red"),
    shape=c(crude="ellipse", budget="circle"),
)

So when you wanted to color the terms by the number of vertexes they have, i'm going to assume you mean edges as each word is a single vertex in the graph. So, using your tdm object

freqterms <- findFreqTerms(tdm, lowfreq = 6)[1:25]
vtxcnt <- rowSums(cor(as.matrix(t(tdm[freqterms,])))>.5)-1

I save the terms you wanted, and then I basically copied the code inside the plot command to calculate the correlations with your cutoff of 0.5 to see how many other words each word in this subset is connected to. That's the vtxcnt variable. (There may be a more efficient way to extract this but I could not find it). Now I'm ready to assign colors

mycols<-c("#f7fbff","#deebf7","#c6dbef",
    "#9ecae1","#6baed6","#4292c6",
    "#2171b5", "#084594")
vc <- mycols[vtxcnt+1]
names(vc) <- names(vtxcnt)

Here I grabbed some colors from ColorBrewer. I have 8 values because the values of vtxcnt range from 0-8. If you had a wider range or wanted to collapsed categories, you could use the cut() command to categorize them. Then I created a named vector vc that matches up each word to the appropriate color. vc should look like this

 head(vc)
 #  ability    accord agreement       ali      also  analysts 
 # "#084594" "#c6dbef" "#2171b5" "#9ecae1" "#f7fbff" "#4292c6" 

And now we are ready to make the plot

pp <- plot(tdm, 
    terms = freqterms, 
    corThreshold = 0.5,
    nodeAttrs=list(fillcolor=vc))

So as you can see the customizing of nodes is pretty flexible. You can color them how every you like if you pass the correct values to nodeAttrs.

colored TermDocumentMatrix Plot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.