Skip to content

Instantly share code, notes, and snippets.

@awhstin
Last active February 16, 2017 15:08
Show Gist options
  • Save awhstin/be6af82ed01afe8c7116b2e740e41402 to your computer and use it in GitHub Desktop.
Save awhstin/be6af82ed01afe8c7116b2e740e41402 to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggplot2)
library(ggthemes)
library(gridExtra)
library(syuzhet)
library(grid)
library(reshape2)
library(viridis)
spells <- read.csv("pathtospells.csv", stringsAsFactors=FALSE)
spells1<-ggplot(spells,aes(x=Position,y=Spell,group=Book,color=Book))+
geom_jitter(alpha=.55)+theme_bw()+theme_tufte()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position="none")
counts<-ggplot(spells,aes(x=Book,fill=Book))+geom_bar()+
geom_text(stat='count',aes(label=..count..),vjust=1,color='white',family='serif')+
geom_text(stat='count',aes(label=spells$Book,color=spells$Book),vjust=-.25,family='serif')+
theme_bw()+theme_tufte()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
legend.position='none')+
annotate('text',x=2,y=129,label='THE SPELLS OF HARRY POTTER',family="serif",fontface=2,cex=5.5)+
annotate('text',x=1.5,y=120,label='A recreation of an already awesome thing',family="serif",cex=2.5)
#spell sentiment
sent<-get_sentiment(spells$Concordance,method=c('bing'))
sent<-t(sent)
sent<-t(sent)
spells$sentiment<-sent
summary.spells<-aggregate(spells$sentiment ,by=list(spells$Spell),mean)
summary.spells$fill<-ifelse(summary.spells$V1 > 0, 'positive', ifelse(summary.spells$V1< 0, 'negative', 'neutral'))
#sentiment plot
Sentp<- c('#F8766D','white','#619Cff')
orig.sent<-ggplot(summary.spells,aes(x=Group.1, y=V1, fill=fill))+
geom_bar(stat='identity',alpha=.45)+
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), legend.position='none',axis.title.y=element_blank())+
theme_bw()+theme_tufte()+ theme(axis.text.x = element_text(angle = 90, hjust = 1))+labs(x='Spell',y='Mean Sentiment')+
scale_fill_manual(values=Sentp)+ylim(-3,3)+coord_flip()
#emotions
test<-get_nrc_sentiment(spells$Concordance)
summary.spells.e<-aggregate(test ,by=list(spells$Spell),mean)
try<-melt(summary.spells.e,id.vars = c("Group.1"))
#emotion test plot
em<-ggplot(try,aes(x=Group.1,y=value,fill=variable))+geom_bar(stat='identity')+coord_flip()
em+ facet_wrap(~ variable)
#waffle
ggplot(try, aes(x = variable, y = Group.1, fill = value)) +
scale_fill_viridis(name="Severity",option = "D", direction=-1,limits = c(0, max(try$value))) +
geom_tile(color = "white", size = 0.4) + theme_tufte()+labs(x='Emotion',y='Spell')
#All the plots
Palette <- c('dark gray','white','light grey')
counts<-ggplot(spells,aes(x=Book,fill=Book))+geom_bar()+
geom_text(stat='count',aes(label=..count..),vjust=1,color='white',family='serif')+
geom_text(stat='count',aes(label=spells$Book,color=spells$Book),vjust=-.25,family='serif')+
theme_bw()+theme_tufte()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
legend.position='none')+
annotate('text',x=2,y=129,label='THE SPELLS OF HARRY POTTER',family="serif",fontface=2,cex=5.5)+
annotate('text',x=1.5,y=120,label='A recreation of an already awesome thing',family="serif",cex=2.5)
spells1<-ggplot(spells,aes(x=Position,y=Spell,group=Book,color=Book))+
geom_jitter(alpha=.55)+theme_bw()+theme_tufte()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position="none")
sent<-ggplot(summary.spells,aes(x=Group.1, y=V1, fill=fill))+
geom_bar(stat='identity',alpha=.25)+
theme(axis.ticks = element_blank(), axis.text.y = element_blank(), legend.position='none',axis.title.y=element_blank())+
coord_flip()+
theme_bw()+theme_tufte()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
legend.position='none')+scale_fill_manual(values=Palette)+ylim(-3,3)
#blank
rect<-textGrob('')
grid.arrange(counts, rect,spells1,sent,ncol=2, nrow=2,heights=c(3,10),widths=c(6.5,1.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment