#Jess Abramson
#February 19, 2019

library(ggplot2)
library(lubridate)
library(RColorBrewer)

# read in the datasets
d <- read.csv("dad1.csv", header = T)
m <- read.csv("mom1.csv", header = T)
md1 <- read.csv("mom&dad1.csv", header = T)
md2 <- read.csv("mom&dad2.csv", header = T)

# append datasets
all <- rbind(d, m, md1, md2)

all$author[all$Sender == "REDACTED"] <- "Dad"
all$author[all$Sender == "REDACTED"] <- "Mom"
all$author[all$Sender == "REDACTED"] <- "Jess"

# remove newline and tab characters to download as CSV file
all$Text <- gsub("\n","\t",all$Text)

all$time <- as.POSIXct(all$Date, format = "%A; %b %d %Y; %H:%M")

all <- all[order(all$time),]
# find texts where I asked a question
all$question <- all$author == "Jess" & grepl("\\?",all$Text)

# write CSV file to annotate in excel
write.csv(all, "allTexts.csv", quote = FALSE, row.names = FALSE)

# relevel so I appear on the bottom
all$author <- factor(all$author, levels = c("Mom", "Dad", "Jess"))
# barplot of number of texts over time
ggplot(all[!is.na(all$author),], aes(x = time, fill = author)) + 
  geom_bar(stat = "count", binwidth = 86400, position = "stack") + 
  ggtitle("Texts sent per day") + 
  theme(plot.title = element_text(hjust = 0.5)) 

# read in annotated texts
annotated <- read.csv("allTextsAnnotated3.csv", header = T)
annotated$time <- as.POSIXct(annotated$Date, format = "%A; %b %d %Y; %H:%M")
# make date variable
annotated$month <- month(as.Date(annotated$time))
annotated$month[annotated$month == 9] <- "September"
annotated$month[annotated$month == 10] <- "October"
annotated$month[annotated$month == 11] <- "November"
annotated$month[annotated$month == 12] <- "December"
annotated$month[annotated$month == 1] <- "January"
annotated$month <- factor(annotated$month,
                          levels = c("September", "October", "November", "December", "January"),)

# function to make well-separated colors
gg_color <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
# create color scale so charts of each topic have consistent colors for each level
myColors <- gg_color(7)
names(myColors) <- levels(annotated$Type)
colScale <- scale_fill_manual(name = "Type",values = myColors)

# Charts of each topic by time (colors show question type)
ggplot(annotated[annotated$Topic == "Food/cooking",], aes(x = month, fill= Type)) + 
  geom_bar(stat = "count", position = "stack") + 
  colScale + 
  ggtitle("Food/cooking") + 
  theme(plot.title = element_text( hjust = 0.5))

ggplot(annotated[annotated$Topic == "Financial",], aes(x = month, fill= Type)) + 
  geom_bar(stat = "count", position = "stack") +
  colScale + 
  ggtitle("Financial") +
  theme(plot.title = element_text( hjust = 0.5))

ggplot(annotated[annotated$Topic == "Social/human",], 
       aes(x = month, fill= Type)) + 
  geom_bar(stat = "count", position = "stack") + 
  colScale + ggtitle("Social/human") + 
  theme(plot.title = element_text( hjust = 0.5))

ggplot(annotated[annotated$Topic == "Home/maintenance",], aes(x = month, fill= Type)) + 
  geom_bar(stat = "count", position = "stack") + 
  colScale + 
  ggtitle("Home/maintenance") + 
  theme(plot.title = element_text( hjust = 0.5))

ggplot(annotated[annotated$Topic == "Family/plans",], aes(x = month, fill= Type)) +
  geom_bar(stat = "count", position = "stack")  +
  ggtitle("Family/plans") +
  theme(plot.title = element_text( hjust = 0.5)) + 
  colScale

ggplot(annotated[annotated$Topic == "Medical",], aes(x = month, fill= Type)) + 
  geom_bar(stat = "count", position = "stack") +
  colScale + 
  ggtitle("Medical") + 
  theme(plot.title = element_text( hjust = 0.5))

# trend lines by frequency and topic
myColors <- brewer.pal(6, "Set1")
names(myColors) <- levels(annotated$Topic)
colScale2 <- scale_color_manual(name = "Topic",values = myColors)

ggplot(annotated, aes(x = month, group = Topic, color = Topic)) + 
  geom_line(stat = "count") + 
  colScale2 + 
  ggtitle("Question Topic Over Time") + 
  theme(plot.title = element_text( hjust = 0.5))

ggplot(annotated[annotated$Topic != "Medical",], aes(x = month, group = Topic, color = Topic)) + 
  colScale2 + 
  geom_line(stat = "count") +  
  ggtitle("Question Topic Over Time - Medical Excluded") +
  theme(plot.title = element_text( hjust = 0.5))

# analysis of type of question over time regardless of topic
ggplot(annotated, aes(x = month, group = Type, fill = Type)) + 
  geom_area(stat = "count") + 
  ggtitle("Question Type Over Time") + 
  colScale + 
  theme(plot.title = element_text( hjust = 0.5))