Skip to content

Instantly share code, notes, and snippets.

View awhstin's full-sized avatar
🦬

Austin Wehrwein awhstin

🦬
View GitHub Profile
library(tidyverse)
library(ggraph)
sw<-starwars%>%
unnest(films)%>%
mutate(movie=films)%>%
select(c(1,11,12))%>%
rename(from=name,to=films)%>%
igraph::graph_from_data_frame()
ggraph(sw,layout='fr')+
@awhstin
awhstin / image_tile.R
Last active December 14, 2017 15:49
Simple image tile for formattable in R
image_tile <- formattable::formatter("img",
src=x~x,
height=25,
width=25,
NA)
@awhstin
awhstin / col_compare.R
Last active July 26, 2017 16:42
simple function to return the column name with the max (or min) value from a comparison of columns.
data(mtcars)
col_compare<-function(df,cols,fun=which.max){
x<-data.frame(df)
temp<-x[,c(cols)]
wins<-colnames(temp)[apply(temp,1,fun)]
return(wins)
}
mtcars$max<-col_compare(mtcars,3:5)
@awhstin
awhstin / europe.R
Created April 24, 2017 14:54 — forked from halhen/europe.R
# data from http://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/population-distribution-demography/geostat
# Originally seen at http://spatial.ly/2014/08/population-lines/
# So, this blew up on both Reddit and Twitter. Two bugs fixed (southern Spain was a mess,
# and some countries where missing -- measure twice, submit once, damnit), and two silly superflous lines removed after
# @hadleywickham pointed that out. Also, switched from geom_segment to geom_line.
# The result of the code below can be seen at http://imgur.com/ob8c8ph
library(tidyverse)
library(acs)
library(ggplot2)
library(plyr)
library(tidyr)
library(maps)
library(viridis)
library(hrbrthemes)
#key
api.key.install(key="pasteyourkeyhere")
library(rvest)
library(tidytext)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
library(viridis)
library(ggthemes)
library(syuzhet)
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')
#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')
@awhstin
awhstin / HPshort.r
Last active February 16, 2017 15:08
library(dplyr)
library(ggplot2)
library(ggthemes)
library(gridExtra)
library(syuzhet)
library(grid)
library(reshape2)
library(viridis)
spells <- read.csv("pathtospells.csv", stringsAsFactors=FALSE)
library(acs)
library(tigris)
library(ggplot2)
library(dplyr)
library(maps)
library(ggthemes)
library(viridis)
library(extrafont)
#only need to do this once
api.key.install(key="YOUR API KEY HERE")