Charts, R, Text Mining, Twitter

Sentiment Heatmap in R

In this post we will build a heatmap using tweets of which we have scored the sentiment in a previous post.

The heatmap plots the sentiment of tweets per day and hour.

In our previous post we assessed the sentiment of tweets like so:

libs <- c("twitteR", "ggplot2")
lapply(libs, library, character.only=TRUE)


#Pull 2000 tweets in English mentioning the term "lobster"
tweets <- searchTwitter("lobster", n=2000, lang="en")

#unlist tweets to data.frame
df <- do.call("rbind", lapply(tweets, as.data.frame))

#Load Lexicons & Algorithm
pos <- readLines ('D:/positive-words.txt')
neg <- readLines ('D:/negative-words.txt')

#Source Algorithm
scores <- score.sentiment(df$text, pos, neg, .progress='text')

Once we have the sentiment score we’ll append it to our data frame of tweets.

df$scores <- scores$score

Now we need to deal with the date format – chage it from character to POSIXct POSIXt. We then need to extract the time. and create the variables for both axis.

#change date format
df$create_time <- as.POSIXct(as.character(df$created),
                                 format = "%Y-%m-%d %H:%M:%S")

#create y-axis breaks and labels
split <- strsplit(df$hm,":")
split <- ldply(split)
df.tm <- cbind(df.tm, split[1])

#rename last column
names(df.tm)[ncol(df.tm)] <- "hour"   

#create y-axis breaks and labels
df.tm$lab <- with(df.tm, paste(df.tm$hour, "00", sep = ":"))

#Aggregate sentiment by date and hour
senti <- aggregate(x=df.tm$score, by=list(date=df.tm$create_time, time=df.tm$lab), FUN=mean)

Great now we can plot the heatmap using the beloved ggplot2 pakcage (thanks Hadley).

ggplot(data = senti, aes(x = date, y = time)) +
  geom_tile(aes(fill=x),colour="grey5") +
  scale_y_discrete(breaks = senti$time) +
  scale_fill_gradient2(low = "#FF0000", midpoint=0,
                       space="Lab", mid="#FFE500", high = "#09B505") +
  theme(axis.text.y = element_text(colour="white"),
        axis.text.x = element_text(colour="white"),
        axis.ticks.y = element_line(colour="grey5"),
        axis.ticks.x = element_line(colour="grey5"),
        axis.title.x = element_text(colour="white"),
        axis.text.y = element_text(colour="white"),
        panel.grid.minor.x = element_line(colour="grey5"),
        panel.grid.major.x = element_line(colour="grey5"),
        panel.grid.minor.y = element_line(colour="grey5"),
        panel.grid.major.y = element_line(colour="grey5"),
        legend.background = element_rect(fill = "grey5"),
        legend.text = element_text(colour="white"),
        legend.title = element_text(colour="white"))

Patek_Unique sentiment heatmap

EDIT: Discretised scores




  1. Pingback: Sentiment Heatmap in R – EDIT: Discretise | SocialFunction() - November 19, 2014

  2. Pingback: Sentiment Heatmap in R - EDIT: Discretise - Jabber Cruncher - May 24, 2015

  3. Pingback: Sentiment Heatmap in R - Jabber Cruncher - May 24, 2015


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: