In this post I want to teach you how to see which users talk about something on Twitter and present it in a simple chart. For this example we are going to look at the hashtag #nintendo,
As we saw in another Post , the first thing we have what to do is make the query on Twitter and save it in a data frame.
library(twitteR)
api_key <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
api_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
setup_twitter_oauth(api_key,api_secret)
## [1] "Using browser based authentication"
Consulta = searchTwitter("#nintendo", geo=NULL, since="2017-05-10", until="2017-05-15")
Datos_Tweets = twListToDF(Consulta)
Consulta_data_frame = do.call("rbind", lapply(Consulta, as.data.frame))
We create the user variables, selecting the data frame users and order them from largest to smallest.
Consulta_transformada = twListToDF(Consulta)
Usuarios = table(Consulta_transformada$screenName)
Usuarios_ordenados = sort(Usuarios, decreasing=T)
To present this data in a visual way we create a color palette with a gradient.
degradado = function (color1, color2, degradados)
{
library(grDevices)
palete = colorRampPalette(c(color1, color2))
palete (degradados)
}
paleta = degradado ("#033240", "#0895BF", 15)
To be able to visualize this data we are going to create a graph, broken down into three parts: 1º We establish the margins to adjust the image 2º We draw the graph with the 15 most active users. 3º We add a legend explaining the data.
## Establezco los márgenes para ajustar la imagen y el color de fondo
par(mar=c(10,5,2,2), bg="white")
## Dibujo un plot de los 15 usuarios TOP
barplot(Usuarios_ordenados[1:15], las=2, cex.names =1, col=paleta)
## we created the last function to get the last entry and its date
last = function(x) { tail(x, n = 1) }
## Incluyo una leyenda con los datos de la búsqueda.
hashtag = "Nintendo"
legend("topright", title=paste("Users More active in ",hashtag, sep=""), legend=paste(last(Consulta_transformada$created), " hasta ",Consulta_transformada$created[1]), text.col="#FFFFFF", bg="#333333B2", inset=0)
And we get a graphic representation of the users who most Tweet with the hashtag # Nintendo.
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});

