
# read in and properly format the data file - has one row per pain instance and date/time of pain
p <- read.csv(filename, header = T)
p$daytime = paste(p$DATE, p$TIME)
# make time stamps read as time to R
p$daytime = as.POSIXct(p$daytime, format = "%Y-%m-%d %I:%M:%S %p")
origintime = as.POSIXct("2019-12-18 12:00:00 PM", format = "%Y-%m-%d %I:%M:%S %p")
p$timesinceorigin = difftime(p$daytime, origintime, units = "days")
p$day = floor(p$timesinceorigin)
p$time = as.numeric(p$timesinceorigin)%%1
row.names(p) = 1:nrow(p)

# make new data frame with one row per day
d = as.data.frame(seq(as.Date("2019/12/18"), by = "day", length.out = 465))
colnames(d) = "date"
d$count1 = NA

# find the number of pain instances within 1 day of each date
for (i in 1:nrow(d)) {
  d$count1[i] = length(which(difftime(d$date[i], p$DATE, units = "days") < 1 &
                               difftime(d$date[i], p$DATE, units = "days") >= 0))
}

# find the number of pain instances within 7 days of each date
d$count7 = NA
for (i in 1:nrow(d)) {
  d$count7[i] = length(which(difftime(d$date[i], p$DATE, units = "days") < 7 &
                               difftime(d$date[i], p$DATE, units = "days") >= 0))
}

library("ggplot2")

# calculate the row and column to have days organized in rows and columns like a calendar
d$index = 0:(nrow(d)-1)
d$row = 8 - floor(d$index/52)
d$col = d$index %% 52
# add a little noise to the point sizes
d$count1wiggle = d$count1 + rnorm(nrow(d), 0, 0.2)

# Plot the data!
ggplot(d[d$count1 != 0,], aes(x = col, y = row, color = count7+1)) +
  geom_point(size = 1 + (d$count1wiggle[d$count1 != 0])*1.1) +
  theme_void() +
  # use a void theme and a black background
  theme(legend.position = "none", plot.background = element_rect(fill = 'black')) +
  # specify the high and low colors in the gradient
  scale_color_gradient(low = "#ffc15e", high = "#fc3a95")

