Twitter Trends Analysis for #26J

Twitter data can be very powerful for trending topic analysis and make predictions about the latest events in the world. In R, we can analyze these trends very easily, with twitteR library. This package allows us download twitter data by hashtag or text providing user, location, date, if retweeted, retweet counts, etc. Here we show an example about trend of #26J hashtag about Spanish elections 2016.

We will need to configure twitter authentication credentials which can be obtained creating an app on twitter in the follow website


consumer_key <- 'xxxxxxxxxxxxx'
consumer_secret <- 'xxxxxxxxxxxxx'
access_token <- 'xxxxxxxxxxxxx'
access_secret <- 'xxxxxxxxxxxxx'

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

Then we download tweets we are interesting for and convert them to data frames to make work easier.

# Download Tweets
tweets <- searchTwitter("#26J", lang="es", n=1000)

# Convert to data frame
df <- twListToDF(tweets)

ggplot(df, aes(x=created)) +
geom_histogram(aes(y=..count..), fill="#1B9E77", color="#1B9E77", alpha="0.3", size=1.1) +
labs(x="Hour", y="Number of tweets / 1000", title = "Trends #26J") +



In the next step, we are going to plot the different trends for #26J hashtag by the four most influential political forces: PP, PSOE, C’S, Podemos (Unidos Podemos).

# Create variable for groups
df$group <- ifelse(grepl("PP", dt.text, = T) | +
grepl("Rajoy", dt.text, = T), "PP",
ifelse(grepl("PSOE", dt.text, = T) | +
grepl("Sanchez", dt.text, = T), "PSOE",
ifelse(grepl("CS", dt.text, = T) | +
grepl("Rivera", dt.text, = T), "CS",
ifelse(grepl("PODEMOS", dt.text, = T) | +
grepl("Iglesias", dt.text, = T), "Podemos", "Neutral"))))

# Set colours
color.df <- brewer.pal(5,"Set1")[c(5, 3, 4, 2, 1)]

ggplot(df, aes(x=created, fill=group, colour=group)) +
geom_density(aes(y=..count..*100),adjust=0.5, position="identity", alpha=0.15, size=1) +
labs(x="Hour", y="Number of tweets / 1000", title = "Trends #26J") +
scale_fill_manual(values=color.df) +
scale_colour_manual(values=color.df) +


ggplot(df, aes(group, fill=group)) +
geom_bar() +
scale_fill_manual(values=color.df) +
labs(x="Political Forces", y="Number of tweets / 1000", title = "Trends #26J") +




Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s