

# clear the workspace and load in the required packages
rm(list = ls())
library("ggplot2")

# read in the data
f = read.csv("datafile.csv",sep = "|", skip = 3)

# now I added a name column and put in the names of close friends based on the phone numbers
f$name = NA
f$name[f$Contact == "123.456.7899"] = "Friend1"
# etc.

j = f[!is.na(f$name),] # just keep the named entries
j$time = as.POSIXct(j$Date...Time, format = "%m/%d/%Y %I:%M:%S %p") # read the time as time
j$date = as.Date(j$Date...Time, format = "%m/%d/%Y") # create a date column with just the dates
j$Hours = j$Minutes/60 # calculate time spent talking in hours

j = j[order(j$time),] # put in order of calls


j$namegroup = NA
j$namegroup[j$name %in% c("Friend1","Friend2")] = "Friends"

l = j[j$Minutes > 1,] # 1 minute phone calls are often missed calls



# Graph just the axes and labels
ggplot(l[!is.na(l$namegroup) & l$date > "2024-01-01",], aes(x = date, y = Hours)) +
  geom_bar(stat = "identity", position = "stack", fill = "#02B7EB") + 
  ggtitle("Total Phone Call Time with Friends Per Day") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_date(breaks = c(seq.Date(as.Date("2022-03-01"), as.Date("2023-06-01"),
                                   by = "1 month")), # make axis show each month
               date_labels = "%b '%y",
               limits = as.Date(c("2022-02-05", "2023-06-08")), expand = c(0,0)) # specify limits of graph
# Add annotations for major events
geom_point(aes(x = as.Date("2022-04-06"), y = 0)) +
  geom_point(aes(x = as.Date("2022-04-17"), y = 0)) +
  geom_point(aes(x = as.Date("2022-05-14"), y = 0)) +
  annotate("text", label = "Turned down\ngrad school offer",
           x = as.Date("2022-03-10"), y = 2.5, angle = 30) +
  annotate("text", label = "ER for\nintractable\nmigraine",
           x = as.Date("2022-04-17"), y = 3.5) +
  annotate("text", label = "Breakup",
           x = as.Date("2022-06-01"), y = 2.7, angle = 0) +
  geom_segment(aes(x = as.Date("2022-04-05"), y = 0.1, xend = as.Date("2022-03-20"), yend = 2.2)) +
  geom_segment(aes(x = as.Date("2022-04-17"), y = 0.1, xend = as.Date("2022-04-17"), yend = 3)) +
  geom_segment(aes(x = as.Date("2022-05-15"), y = 0.1, xend = as.Date("2022-05-29"), yend = 2.55)) +
  ylim(c(0,3.8))

# graph the data through the breakup
ggplot(l[!is.na(l$namegroup) & l$date < as.Date("2022-05-14"),], aes(x = date, y = Hours)) +
  geom_bar(stat = "identity", position = "stack", fill = "#02B7EB") + 
  ggtitle("Total Phone Call Time with Friends Per Day") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_date(breaks = c(seq.Date(as.Date("2022-03-01"), as.Date("2023-06-01"),
                                   by = "1 month")),
               date_labels = "%b '%y",
               limits = as.Date(c("2022-02-05", "2023-06-08")), expand = c(0,0)) +
  geom_point(aes(x = as.Date("2022-04-06"), y = 0)) +
  geom_point(aes(x = as.Date("2022-04-17"), y = 0)) +
  geom_point(aes(x = as.Date("2022-05-14"), y = 0)) +
  annotate("text", label = "Turned down\ngrad school offer",
           x = as.Date("2022-03-10"), y = 2.5, angle = 30) +
  annotate("text", label = "ER for\nintractable\nmigraine",
           x = as.Date("2022-04-17"), y = 3.5) +
  annotate("text", label = "Breakup",
           x = as.Date("2022-06-01"), y = 2.7, angle = 0) +
  geom_segment(aes(x = as.Date("2022-04-05"), y = 0.1, xend = as.Date("2022-03-20"), yend = 2.2)) +
  geom_segment(aes(x = as.Date("2022-04-17"), y = 0.1, xend = as.Date("2022-04-17"), yend = 3)) +
  geom_segment(aes(x = as.Date("2022-05-15"), y = 0.1, xend = as.Date("2022-05-29"), yend = 2.55)) +
  ylim(c(0,3.8))

# graph through September
ggplot(l[!is.na(l$namegroup) & l$date < as.Date("2022-10-01"),], aes(x = date, y = Hours)) +
  geom_bar(stat = "identity", position = "stack", fill = "#02B7EB") + 
  ggtitle("Total Phone Call Time with Friends Per Day") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_date(breaks = c(seq.Date(as.Date("2022-03-01"), as.Date("2023-06-01"),
                                   by = "1 month")),
               date_labels = "%b '%y",
               limits = as.Date(c("2022-02-05", "2023-06-08")), expand = c(0,0)) +
  geom_point(aes(x = as.Date("2022-04-06"), y = 0)) +
  geom_point(aes(x = as.Date("2022-04-17"), y = 0)) +
  geom_point(aes(x = as.Date("2022-05-14"), y = 0)) +
  annotate("text", label = "Turned down\ngrad school offer",
           x = as.Date("2022-03-10"), y = 2.5, angle = 30) +
  annotate("text", label = "ER for\nintractable\nmigraine",
           x = as.Date("2022-04-17"), y = 3.5) +
  annotate("text", label = "Breakup",
           x = as.Date("2022-06-01"), y = 2.7, angle = 0) +
  geom_segment(aes(x = as.Date("2022-04-05"), y = 0.1, xend = as.Date("2022-03-20"), yend = 2.2)) +
  geom_segment(aes(x = as.Date("2022-04-17"), y = 0.1, xend = as.Date("2022-04-17"), yend = 3)) +
  geom_segment(aes(x = as.Date("2022-05-15"), y = 0.1, xend = as.Date("2022-05-29"), yend = 2.55)) +
  ylim(c(0,3.8))

# graph all the data
ggplot(l[!is.na(l$namegroup),], aes(x = date, y = Hours)) +
  geom_bar(stat = "identity", position = "stack", fill = "#02B7EB") + 
  ggtitle("Total Phone Call Time with Friends Per Day") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_date(breaks = c(seq.Date(as.Date("2022-03-01"), as.Date("2023-06-01"),
                                   by = "1 month")),
               date_labels = "%b '%y",
               limits = as.Date(c("2022-02-05", "2023-06-08")), expand = c(0,0)) +
  geom_point(aes(x = as.Date("2022-04-06"), y = 0)) +
  geom_point(aes(x = as.Date("2022-04-17"), y = 0)) +
  geom_point(aes(x = as.Date("2022-05-14"), y = 0)) +
  annotate("text", label = "Turned down\ngrad school offer",
           x = as.Date("2022-03-10"), y = 2.5, angle = 30) +
  annotate("text", label = "ER for\nintractable\nmigraine",
           x = as.Date("2022-04-17"), y = 3.5) +
  annotate("text", label = "Breakup",
           x = as.Date("2022-06-01"), y = 2.7, angle = 0) +
  geom_segment(aes(x = as.Date("2022-04-05"), y = 0.1, xend = as.Date("2022-03-20"), yend = 2.2)) +
  geom_segment(aes(x = as.Date("2022-04-17"), y = 0.1, xend = as.Date("2022-04-17"), yend = 3)) +
  geom_segment(aes(x = as.Date("2022-05-15"), y = 0.1, xend = as.Date("2022-05-29"), yend = 2.55)) +
  ylim(c(0,3.8))


