Skip to content

Instantly share code, notes, and snippets.

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

  • Save audhiaprilliant/22585268dfe094fdd4c2bc6c73f9f60d to your computer and use it in GitHub Desktop.

Select an option

Save audhiaprilliant/22585268dfe094fdd4c2bc6c73f9f60d to your computer and use it in GitHub Desktop.
Twitter Data Visualization using ggplot2
# JOKO WIDODO
df.senti.3 = as.data.frame(table(senti.jokowi$class))
colnames(df.senti.3) = c('Sentiment','Freq')
# Data pre-processing
df.pie.1 = df.senti.3
df.pie.1$Prop = df.pie.1$Freq/sum(df.pie.1$Freq)
df.pie.1 = df.pie.1 %>%
arrange(desc(Sentiment)) %>%
mutate(lab.ypos = cumsum(Prop) - 0.5*Prop)
# Data viz
ggplot(df.pie.1,
aes(x = 2,
y = Prop,
fill = Sentiment))+
geom_bar(stat = 'identity',
col = 'white',
alpha = 0.75,
show.legend = TRUE)+
coord_polar(theta = 'y',
start = 0)+
geom_text(aes(y = lab.ypos,
label = Prop),
color = 'white',
fontface = 'italic',
size = 4)+
labs(title = 'Piechart of Sentiments',
subtitle = 'Joko Widodo',
caption = 'Twitter Crawling 28 - 29 May 2019')+
xlim(c(0.5,2.5))+
theme_void()+
scale_fill_brewer(palette = 'Dark2')+
theme(legend.title = element_blank(),
legend.position = 'right')
# PRABOWO SUBIANTO
df.senti.4 = as.data.frame(table(senti.prabowo$class))
colnames(df.senti.4) = c('Sentiment','Freq')
# Data pre-processing
df.pie.2 = df.senti.4
df.pie.2$Prop = df.pie.2$Freq/sum(df.pie.2$Freq)
df.pie.2 = df.pie.2 %>%
arrange(desc(Sentiment)) %>%
mutate(lab.ypos = cumsum(Prop) - 0.5*Prop)
# Data viz
ggplot(df.pie.2,
aes(x = 2,
y = Prop,
fill = Sentiment))+
geom_bar(stat = 'identity',
col = 'white',
alpha = 0.75,
show.legend = TRUE)+
coord_polar(theta = 'y',
start = 0)+
geom_text(aes(y = lab.ypos,
label = Prop),
color = 'white',
fontface = 'italic',
size = 4)+
labs(title = 'Piechart of Sentiments',
subtitle = 'Prabowo Subianto',
caption = 'Twitter Crawling 28 - 29 May 2019')+
xlim(c(0.5,2.5))+
theme_void()+
scale_fill_brewer(palette = 'Dark2')+
theme(legend.title = element_blank(),
legend.position = 'right')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment