In [141]:
options(warn = -1)
# IMPORTANT: This assumes that all packages in "Rstart.R" are installed,
# and the fonts "Source Sans Pro" and "Open Sans Condensed Bold" are installed
# via extrafont. If ggplot2 charts fail to render, you may need to change/remove the theme call.
source("Rstart.R")
sessionInfo()
Out[141]:
In [142]:
df_donations <- read_csv("agdq_donations.csv")
df_comments <- read_csv("agdq_comments.csv")
df_runs <- read_csv("agdq_runs.csv")
In [143]:
print(df_donations %>% head())
nrow(df_donations)
Out[143]:
In [144]:
getID <- function(x) {
return(strsplit(x,"/")[[1]][6])
}
getID("https://gamesdonequick.com/tracker/donor/20093/agdq2016")
Out[144]:
In [145]:
df_donations <- df_donations %>% mutate(id = sapply(amount.href, getID), donor_id = sapply(name.href, getID))
print(df_donations %>% select(id, donor_id) %>% head())
Date Format is: January 9th, 2016, 8:11:59 PM
This is nonstandard and required hacky code to fix. You have been warned!
In [146]:
formatDate <- function(x) {
tryCatch({
day_split <- strsplit(x, " ")[[1]]
day_part <- day_split[2]
# if single digit, numeric component of day must have a leading zero
day_leading <- sprintf("%02s",substring(day_part,1,nchar(day_part)-3))
day_split[2] <- day_leading
day_string_new <- paste(day_split, collapse=" ")
day_date <- strptime(day_string_new, "%B %d %Y, %r")
return (as.POSIXct(day_date))
},
error = function(x) {return(NA)})
}
formatDate("January 9th, 2016, 8:11:59 PM")
Out[146]:
In [147]:
df_donations <- df_donations %>% mutate(time_donated = sapply(time, formatDate))
print(df_donations %>% select(time_donated) %>% head())
In [148]:
getAmount <- function(x) {
num <- gsub(",", "", x)
return(as.numeric(substr(num,2,nchar(num))))
}
getAmount("$1,024.99")
Out[148]:
In [149]:
df_donations <- df_donations %>% mutate(amount_donated = sapply(amount.text, getAmount))
print(df_donations %>% select(amount_donated) %>% head())
In [150]:
df_donations <- df_donations %>% select(id, donor=name.text, amount_donated, time_donated, donor_id)
print(df_donations %>% head())
In [151]:
print(df_comments %>% head())
nrow(df_comments)
Out[151]:
In [152]:
df_comments <- df_comments %>%
mutate(id = sapply(url, getID)) %>%
select(id, comment, bid_category = bid_category,
bid_choice = bid_choice)
print(df_comments %>% head())
In [153]:
print(df_runs %>% head())
nrow(df_runs)
Out[153]:
In [154]:
df_runs <- df_runs %>%
mutate(start_time = sapply(start_time, formatDate), end_time = sapply(end_time, formatDate)) %>%
select(run_title = run_title.text, start_time, end_time)
print(df_runs %>% head())
In [157]:
df <- df_donations %>% left_join(df_comments, by="id") %>%
select(id, donor_id, donor, amount_donated, time_donated, comment, bid_category, bid_choice)
# Fix a few problematic donations manually
df[which(df$id==212588),7:8] <- c("Super Mario Maker Custom Level Blind Race", "Mario Maker Hard Level race")
df[which(df$id==215656),7:8] <- c("The Legend Of Zelda: Majora's Mask 4p Co-Op 100%", "Blindforded Co-op Majora Fight")
print(df %>% head())
In [158]:
whenRunDonated <- function(x) {
# if donation is > start time and < end time, that was when the donation was made
run_truth_table = (x > df_runs$start_time) & (x < df_runs$end_time)
if (sum(run_truth_table, na.rm=T) > 0) {return (df_runs$run_title[which(run_truth_table)])}
else {return (NA)}
}
whenRunDonated(1452389135)
Out[158]:
In [159]:
df <- df %>% mutate(run_during_donated = sapply(time_donated, whenRunDonated))
print(df %>% select(run_during_donated) %>% head())
Done!
In [160]:
df_write <- df
df_write <- df_write %>% mutate(time_donated = format(as.POSIXlt(time_donated, origin = "1970-01-01"), "%Y-%m-%d %H:%M:%S"))
df_write[is.na(df_write)] <- ''
write.csv(df_write, "agqd_2016_data.csv", row.names=F)
rm(df_write)
In [185]:
df_cumsum <- df %>% select(time_donated, amount_donated) %>%
filter(!is.na(amount_donated)) %>%
arrange(time_donated) %>%
mutate(time_donated = as.POSIXct(time_donated, origin = "1970-01-01"),
cumsum = cumsum(amount_donated),
day = format(time_donated, "%d"))
print(df_cumsum %>% head())
print(nrow(df_cumsum))
In [186]:
plot <- ggplot(df_cumsum, aes(x=time_donated, y=cumsum, color=day)) +
geom_line() +
fte_theme() +
scale_x_datetime(limits=c(as.POSIXct("2016-01-03 00:00:00"), as.POSIXct("2016-01-10 00:00:00"))) +
scale_y_continuous(label=dollar) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
labs(title="Cumulative $ Amount Raised For Charity by AGDQ 2016")
max_save(plot, "agdq-1", "Games Done Quick")
In [163]:
mean <- mean(df$amount_donated, na.rm=T)
median <- median(df$amount_donated, na.rm=T)
lte_100 <- sum(df$amount_donated <= 100, na.rm=T) / nrow(df)
print(mean)
print(median)
print(lte_100)
In [164]:
plot <- ggplot(df, aes(x=amount_donated)) +
geom_histogram(binwidth=1, fill="#2e8ece") +
fte_theme() +
scale_x_continuous(label=dollar, limits=c(0,101), breaks=seq(0,100, by=10)) +
scale_y_continuous(label=comma) +
labs(title="Histogram of 97% of AGDQ 2016 Donations", x="Donation Amount", y="# of Donations")
max_save(plot, "agdq-2", "Games Done Quick")
In [165]:
twoLine <- function(x, minchar) {
if (nchar(x) < minchar) {return (x)}
middle_index <- round(nchar(x)/2)
space_indices <- unlist(gregexpr(" ", x))
replacement_index = space_indices[which(abs(space_indices-middle_index) == min(abs(space_indices-middle_index)))]
left <- substr(x, 1, replacement_index-1)
right <- substr(x, replacement_index+1, nchar(x))
return(paste(left,right,sep='\n'))
}
df_bidrun <- df %>% group_by(run_during_donated) %>%
summarize(count=n()) %>%
arrange(desc(count)) %>%
head(15) %>%
mutate(run_during_donated = sapply(run_during_donated, twoLine,30)) %>%
mutate(run_during_donated = factor(run_during_donated, levels=rev(run_during_donated)))
print(df_bidrun %>% head())
In [166]:
plot <- ggplot(df_bidrun, aes(x=run_during_donated, y=count)) +
geom_bar(stat="identity", fill="#e74c3c") +
coord_flip() +
fte_theme() +
geom_text(aes(label=format(count, big.mark=",")), color="white", hjust=1.5, family="Open Sans Condensed Bold", size=2.5) +
theme(axis.title.y = element_blank(), axis.text.y = element_text(size=6)) +
labs(y="Total Number of Donations during Run", title="AGDQ 2016 Runs Which Generated Most Donations")
max_save(plot, "agdq-3", "Games Done Quick", h=5)
In [183]:
df_bidrun <- df %>% group_by(run_during_donated) %>%
summarize(total_amount = sum(amount_donated, na.rm=T)) %>%
arrange(desc(total_amount)) %>%
head(15) %>%
mutate(run_during_donated = sapply(run_during_donated, twoLine,30)) %>%
mutate(run_during_donated = factor(run_during_donated, levels=rev(run_during_donated)))
print(df_bidrun %>% head())
In [184]:
plot <- ggplot(df_bidrun, aes(x=run_during_donated, y=total_amount)) +
geom_bar(stat="identity", fill="#2ecc71") +
coord_flip() +
fte_theme() +
scale_y_continuous(labels=dollar) +
geom_text(aes(label=paste0("$",format(round(total_amount), big.mark=","))), color="white", hjust=1.1, family="Open Sans Condensed Bold", size=2.5) +
theme(axis.title.y = element_blank(), axis.text.y = element_text(size=6)) +
labs(y="Total $ Donated During Run", title="AGDQ 2016 Runs Which Most $ Donated")
max_save(plot, "agdq-4", "Games Done Quick", h=5)
In [187]:
df_bidcat <- df %>% group_by(bid_category) %>%
filter(bid_category!='') %>%
summarize(total_amount = sum(amount_donated, na.rm=T)) %>%
arrange(desc(total_amount)) %>%
head(10) %>%
mutate(bid_category = sapply(bid_category, twoLine,30)) %>%
mutate(bid_category = factor(bid_category, levels=rev(bid_category)))
print(df_bidcat %>% head())
In [188]:
left <- c(paste0("$",format(round(df_bidcat$total_amount[1:2]), big.mark=",")), rep('',8))
right <- c(rep('',2), paste0("$",format(round(df_bidcat$total_amount[3:10]), big.mark=",")))
plot <- ggplot(df_bidcat, aes(x=bid_category, y=total_amount)) +
geom_bar(stat="identity", fill="#1abc9c") +
coord_flip() +
fte_theme() +
scale_y_continuous(labels=dollar) +
geom_text(label=left, color="white", hjust=1.1, family="Open Sans Condensed Bold", size=2.5) +
geom_text(label=right, color="#1abc9c", hjust=-0.2, family="Open Sans Condensed Bold", size=2.5) +
theme(axis.title.y = element_blank(), axis.text.y = element_text(size=6)) +
labs(y="Total $ Donated Toward Incentive", title="Top AGDQ 2016 Donation Run Incentives")
max_save(plot, "agdq-5", "Games Done Quick")
In [175]:
df_bidcat <- df %>% group_by(bid_choice) %>%
filter(bid_choice!='') %>%
summarize(total_amount = sum(amount_donated, na.rm=T)) %>%
arrange(desc(total_amount)) %>%
head(10) %>%
mutate(bid_choice = sapply(bid_choice, twoLine,30)) %>%
mutate(bid_choice = factor(bid_choice, levels=rev(bid_choice)))
#cat(df_bidcat[2,1])
print(df_bidcat %>% head())
In [176]:
left <- c(paste0("$",format(round(df_bidcat$total_amount[1:2]), big.mark=",")), rep('',8))
right <- c(rep('',2), paste0("$",format(round(df_bidcat$total_amount[3:10]), big.mark=",")))
plot <- ggplot(df_bidcat, aes(x=bid_choice, y=total_amount)) +
geom_bar(stat="identity", fill="#9b59b6") +
coord_flip() +
fte_theme() +
scale_y_continuous(labels=dollar) +
geom_text(label=left, color="white", hjust=1.1, family="Open Sans Condensed Bold", size=2.5) +
geom_text(label=right, color="#9b59b6", hjust=-0.2, family="Open Sans Condensed Bold", size=2.5) +
theme(axis.title.y = element_blank(), axis.text.y = element_text(size=6)) +
labs(y="Total $ Donated Toward Choice", title="Top AGDQ 2016 Donation Incentive Choices")
max_save(plot, "agdq-6", "Games Done Quick")
In [179]:
stop_words <- unlist(strsplit("a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your", ","))
words.list <- strsplit(tolower(as.character(df$comment)), split=" ")
words.vector <- unlist(words.list)
words.vector <- apply(as.array(words.vector),1,function (x) {return (gsub("[^\\w\\']","",x, perl=T))})
word.freq <- sort(table(words.vector),decreasing=TRUE)
word.freq <- word.freq[-which(names(word.freq) %in% stop_words)]
word.freq <- word.freq[which(word.freq>10)]
print(word.freq[1:10])
In [180]:
library(wordcloud)
pal <- brewer.pal(9, "Blues")
pal <- pal[-c(1:3)] # Remove light colors
png(filename = "agdq-7.png", width = 1000, height = 1000, res= 300)
wordcloud(toupper(names(word.freq)),
word.freq,
scale=c(3,.1),
random.order=F,
rot.per=.10,
max.words=5000,
colors=pal,
family="Source Sans Pro Bold",
random.color=T)
dev.off()
Out[180]:
Copyright (c) 2016 Max Woolf
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.