Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A first look at the sleepdata recorded with the Sleep Cycle app for the iPhone
sleep <- read.csv(file="sleepdata.csv", header=T, sep=";")
#split end time
sleep$End <- as.character(sleep$End)
#get the date
sleep$Date <- sapply(strsplit(sleep$End, " "),"[[",1)
sleep$Date <- strptime(sleep$Date, format="%Y-%m-%d")
#fix sleep quality
sleep$Sleep.quality <- as.integer(gsub("%", "", as.character(sleep$Sleep.quality)))
#convert Time in bed to minutes
StringToMinutes <- function(x) {
# Converts a String with format HH:MM:SS to a decimal minutes representation
# Args:
# x: String with format HH:MM
# Returns:
# Float minutes
x <- as.numeric(x)
x[1]*60 + x[2]
sleep$ <- sapply(strsplit(as.character(sleep$, ":"), StringToMinutes)
#correlation sleep quality and time in bed
p <- ggplot(sleep, aes(, Sleep.quality)) + theme_bw()
p <- p + geom_point(aes(colour=Sleep.Notes)) + ggtitle("Sleep Quality vs. Time in Bed") + xlab("Time in Bed in Minutes") + ylab("Sleep Quality in %")
p + geom_smooth(method=lm)
#plot sleep duration over time
p <- ggplot(sleep, aes(Date, + theme_bw()
p <- p + geom_point(aes(colour=Sleep.quality), size=3.5) + scale_colour_gradient(limits=c(30, 100), low="red", high="green", space="Lab")
p <- p + ggtitle("Sleep Duration Over Time") + xlab("Date") + ylab("Duration in Minutes")
p + geom_smooth(method=loess)
#group sleep quality by month
df <- data.frame(
date = sleep$Date,
x = sleep$Sleep.quality
df$my <- floor_date(df$date, "month")
group <- ddply(df, "my", summarise, x = mean(x))
p <- ggplot(group, aes(my, x)) + theme_bw()
p <- p + ggtitle("Sleep Quality by Month Time in Bed") + xlab("Month") + ylab("Sleep Quality in %")
p + geom_bar(stat="identity", colour="black", fill="lightblue")

This comment has been minimized.

Copy link

zhusleep commented Apr 4, 2016

Hello,Tafkas. Could you please send me a copy of the sleep data? I am using Android so I can not get the data from iphone sleep cycle.
Thanks! My mail:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.