Skip to content

Instantly share code, notes, and snippets.

@audhiaprilliant
Created December 13, 2020 09:37
Show Gist options
  • Select an option

  • Save audhiaprilliant/06e4d31d0ae311dcf2631e0e657dc865 to your computer and use it in GitHub Desktop.

Select an option

Save audhiaprilliant/06e4d31d0ae311dcf2631e0e657dc865 to your computer and use it in GitHub Desktop.
Twitter Data Visualization using ggplot2
# JOKO WIDODO
df.senti.score.1 = data.frame(table(senti.jokowi$score))
colnames(df.senti.score.1) = c('Score','Freq')
# Data pre-processing
df.senti.score.1$Score = as.character(df.senti.score.1$Score)
df.senti.score.1$Score = as.numeric(df.senti.score.1$Score)
Score1 = df.senti.score.1$Score
sign(df.senti.score.1[1,1])
for (i in 1:nrow(df.senti.score.1)) {
sign.row = sign(df.senti.score.1[i,'Score'])
for (j in 1:ncol(df.senti.score.1)) {
df.senti.score.1[i,j] = df.senti.score.1[i,j] * sign.row
}
}
df.senti.score.1$Label = c(letters[1:nrow(df.senti.score.1)])
df.senti.score.1$Sentiment = ifelse(df.senti.score.1$Freq < 0,
'Negative','Positive')
df.senti.score.1$Score1 = Score1
# Data viz
ggplot(df.senti.score.1)+
geom_bar(aes(x = Label,
y = Freq,
fill = Sentiment),
stat = 'identity',
show.legend = FALSE)+
# Positive Sentiment
geom_hline(yintercept = mean(abs(df.senti.score.1[which(df.senti.score.1$Sentiment == 'Positive'),'Freq'])),
col = I('black'),
size = 1)+
geom_text(aes(fontface = 'italic',
label = paste('Average Freq:',
ceiling(mean(abs(df.senti.score.1[which(df.senti.score.1$Sentiment == 'Positive'),'Freq'])))),
x = 10,
y = mean(abs(df.senti.score.1[which(df.senti.score.1$Sentiment == 'Positive'),'Freq']))+30),
hjust = 'right',
size = 4)+
# Negative Sentiment
geom_hline(yintercept = mean(df.senti.score.1[which(df.senti.score.1$Sentiment == 'Negative'),'Freq']),
col = I('black'),
size = 1)+
geom_text(aes(fontface = 'italic',
label = paste('Average Freq:',
ceiling(mean(abs(df.senti.score.1[which(df.senti.score.1$Sentiment == 'Negative'),'Freq'])))),
x = 5,
y = mean(df.senti.score.1[which(df.senti.score.1$Sentiment == 'Negative'),'Freq'])-15),
hjust = 'left',
size = 4)+
labs(title = 'Barplot of Sentiments',
subtitle = 'Joko Widodo',
caption = 'Twitter Crawling 28 - 29 May 2019')+
xlab('Score')+
scale_x_discrete(limits = df.senti.score.1$Label,
labels = df.senti.score.1$Score1)+
theme_bw()+
scale_fill_brewer(palette = 'Dark2')
# PRABOWO SUBIANTO
df.senti.score.2 = data.frame(table(senti.prabowo$score))
colnames(df.senti.score.2) = c('Score','Freq')
# Data pre-processing
df.senti.score.2$Score = as.character(df.senti.score.2$Score)
df.senti.score.2$Score = as.numeric(df.senti.score.2$Score)
Score2 = df.senti.score.2$Score
sign(df.senti.score.2[1,1])
for (i in 1:nrow(df.senti.score.2)) {
sign.row = sign(df.senti.score.2[i,'Score'])
for (j in 1:ncol(df.senti.score.2)) {
df.senti.score.2[i,j] = df.senti.score.2[i,j] * sign.row
}
}
df.senti.score.2$Label = c(letters[1:nrow(df.senti.score.2)])
df.senti.score.2$Sentiment = ifelse(df.senti.score.2$Freq < 0,
'Negative','Positive')
df.senti.score.2$Score1 = Score2
# Data viz
ggplot(df.senti.score.2)+
geom_bar(aes(x = Label,
y = Freq,
fill = Sentiment),
stat = 'identity',
show.legend = FALSE)+
# Positive Sentiment
geom_hline(yintercept = mean(abs(df.senti.score.2[which(df.senti.score.2$Sentiment == 'Positive'),'Freq'])),
col = I('black'),
size = 1)+
geom_text(aes(fontface = 'italic',
label = paste('Average Freq:',
ceiling(mean(abs(df.senti.score.2[which(df.senti.score.2$Sentiment == 'Positive'),'Freq'])))),
x = 11,
y = mean(abs(df.senti.score.2[which(df.senti.score.2$Sentiment == 'Positive'),'Freq']))+20),
hjust = 'right',
size = 4)+
# Negative Sentiment
geom_hline(yintercept = mean(df.senti.score.2[which(df.senti.score.2$Sentiment == 'Negative'),'Freq']),
col = I('black'),
size = 1)+
geom_text(aes(fontface = 'italic',
label = paste('Average Freq:',
ceiling(mean(abs(df.senti.score.2[which(df.senti.score.2$Sentiment == 'Negative'),'Freq'])))),
x = 9,
y = mean(df.senti.score.2[which(df.senti.score.2$Sentiment == 'Negative'),'Freq'])-10),
hjust = 'left',
size = 4)+
labs(title = 'Barplot of Sentiments',
subtitle = 'Prabowo Subianto',
caption = 'Twitter Crawling 28 - 29 May 2019')+
xlab('Score')+
scale_x_discrete(limits = df.senti.score.2$Label,
labels = df.senti.score.2$Score1)+
theme_bw()+
scale_fill_brewer(palette = 'Dark2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment