0
votes

I am creating a social network graph that is based on a Twitter dataset. I calculated sentiment analysis scores for the tweets and I wanted to color-code the nodes according to the sentiment score. There are 7 different categorical values that I want to use I want to have a different color to represent each sentiment score. The sentiment values are "neutral," "somewhat positive," "positive," "very positive," "somewhat negative," "negative," and "very negative." I am using igraph in R to accomplish this and I got a simpler version with just two sentiment states working, i.e. "positive" and "negative."Here was the code that I used for that graph:

V(rt_graph)$color <- ifelse(tweets[V(rt_graph), 2] == "positive", "blue", "red")

I was attempting to assign a different color to each node of a different sentiment value, but I have not been successful in figuring out how to do that. Could someone please put me in the right direction? Here is what I was trying to work with:

V(rt_graph)$color(tweets$score[V(rt_graph) == 'Negative']) <- 'orangered3'
1

1 Answers

0
votes

You don't provide any data so I will illustrate with a synthetic graph.

library(igraph)
set.seed(1234)
g = erdos.renyi.game(40, 0.15)
SLevels = c("neutral", "somewhat positive", "positive", 
    "very positive", "somewhat negative", "negative", "very negative" )
sentiment = factor(sample(SLevels, 40, replace=T))

Now to assign seven different colors for the seven levels of sentiment, you can just use:

ColorList = c("yellow", "darkblue", "lightblue", "green", 
    "orange", "pink", "red")
V(g)$color = ColorList[sentiment]
plot(g, vertex.size=10, vertex.label="")

Graph with multi-colored nodes

Since sentiment was assigned at random in my graph, it has no real meaning. Hopefully, in your data it will make more sense.