Skip to content

Instantly share code, notes, and snippets.

@rer145
Last active November 20, 2017 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rer145/3f6ac0c926b2a71713725bd07d49a8bb to your computer and use it in GitHub Desktop.
Save rer145/3f6ac0c926b2a71713725bd07d49a8bb to your computer and use it in GitHub Desktop.
Creating a slide presentation with beamer and latex
\documentclass{beamer}
\begin{document}
\title{Positive and Negative Words in Dracula}
\author{Ron Richardson}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
\section{Install and Load Libraries}
\begin{frame}[fragile]
\frametitle{Install and Load Libraries}
\begin{itemize}
\item<1->
<<warning=FALSE, message=FALSE>>=
library(dplyr)
@
\item<2->
<<warning=FALSE, message=FALSE>>=
library(gutenbergr)
@
\item<3->
<<warning=FALSE, message=FALSE>>=
library(tidytext)
@
\item<4->
<<warning=FALSE, message=FALSE>>=
library(stringr)
@
\item<5->
<<warning=FALSE, message=FALSE>>=
library(ggplot2)
@
\end{itemize}
\end{frame}
\section{Searching for a Novel}
\begin{frame}[fragile]
\frametitle{Searching for a Novel}
<<warning=FALSE, message=FALSE>>=
df<-gutenberg_works(str_detect(title, 'Dracula'))
df$gutenberg_id
df$title
@
\end{frame}
\section{Download Dracula from Project Gutenberg}
\begin{frame}[fragile]
\frametitle{Download Dracula from Project Gutenberg}
<<warning=FALSE, message=FALSE>>=
dracula<-gutenberg_download(345)
colnames(dracula)
substr(dracula$text[500], 1, 21)
@
\end{frame}
\section{Unpack the Words}
\begin{frame}[fragile]
\frametitle{Unpack the Words}
<<warning=FALSE, message=FALSE>>=
words<-dracula%>%
unnest_tokens(word, text)
colnames(words)
words[500,]
@
\end{frame}
\section{The Bing Lexicon}
\begin{frame}[fragile]
\frametitle{The Bing Lexicon}
<<warning=FALSE, message=FALSE>>=
bing<-get_sentiments('bing')
colnames(bing)
bing[500,]
@
\end{frame}
\section{Joining Dracula with Bing}
\begin{frame}[fragile]
\frametitle{Joining Dracula with Bing}
<<warning=FALSE, message=FALSE>>=
words<-inner_join(words, bing)
words$gutenberg_id<-NULL
colnames(words)
@
\end{frame}
\section{Top 10 Positive Words}
\begin{frame}[fragile, allowframebreaks]
\frametitle{Top 10 Positive Words}
<<warning=FALSE, message=FALSE>>=
words_pos<-words%>%
filter(sentiment=='positive')%>%
group_by(word)%>%
summarize(count=n(), sentiment=first(sentiment))%>%
arrange(count)%>%
top_n(10, wt=count)
@
\framebreak
<<>>=
words_pos
@
\end{frame}
\section{Top 10 Negative Words}
\begin{frame}[fragile]
\frametitle{Top 10 Negative Words}
<<echo=FALSE>>=
words_neg<-words%>%
filter(sentiment=='negative')%>%
group_by(word)%>%
summarize(count=n(), sentiment=first(sentiment))%>%
arrange(count)%>%
top_n(10, wt=count)
words_neg
@
\end{frame}
\section{The Comparison Barplot}
\begin{frame}[fragile, allowframebreaks]
\frametitle{The Comparison Barplot}
<<warning=FALSE, message=FALSE>>=
words_pos$word<-factor(words_pos$word, levels=words_pos$word)
words_neg$word<-factor(words_neg$word, levels=words_neg$word)
words<-rbind(words_pos, words_neg)
@
\framebreak
<<warning=FALSE, message=FALSE>>=
plot<-ggplot()+
geom_bar(data=words, aes(x=word, y=count, fill=sentiment, color=sentiment), stat="identity")+
xlab("Word")+
ylab("Count")+
coord_flip()+
ggtitle("Top 10 Positive/Negative Words in Dracula")+
facet_wrap(~sentiment, scales='free_y')+ # which column(s) distinguishes the groups
scale_fill_manual(values=c('#000000', '#ea6205'))+
scale_color_manual(values=c('#ea6205', '#000000'))
@
\framebreak
<<echo=FALSE, fig.height=4>>=
plot
@
\end{frame}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment