Hi - beginning R user question here - each day, over the course of several months, I've tracked the time I go to bed, the time I wake up, and my hours spent sleeping. What would be a good way to display this information? I think it would be ideal to show something resembling a bar and whisker graph for each day that would show the interval of hours spent asleep (or perhaps just a bar "floating" against a backdrop showing the hours of a given day/night), and that would also have a simple line graph of the total number of hours per day. Can I get a hint perhaps? Thanks very much. [[alternative HTML version deleted]]
Joshua Wiley
2011-Jul-02 02:45 UTC
[R] beginner question - effective way to chart sleep habits
Hi Peter, There are lots and lots of ways. Here are examples of a few that came to mind. If you have never used the ggplot2 package, you will first need to install it, which you can do by typing: install.packages("ggplot2") The code below should run "as is". You may see various trends over time or based on bedtime because the data is (pseudo)randomly generated, but you should see a "weekend" effect, which pops out in the circular plot. Hope this helps, Josh ## Make up some data with potentially interesting weekend pattern Date <- as.POSIXlt(as.POSIXct(1309639554, origin = "1970-01-01") + cumsum(rep(86400, 90)) + rnorm(90, sd = 2700)) d <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") dat <- data.frame(Date, Day = factor(weekdays(Date), levels = d), Bedtime = Date$hour + Date$min/60, Hours = rnorm(length(Date), mean = 6.4)) weekends <- grep("Saturday|Sunday", dat$Day) dat[weekends, "Hours"] <- dat[weekends, "Hours"] + rnorm(length(weekends), 1) tmp <- with(dat, tapply(Hours, Day, mean)) dat2 <- data.frame(Days = factor(names(tmp), levels = d), Hours = as.numeric(tmp)) ## Load required packages require(ggplot2) ## Create three separate plots ## you can look at them individually by calling: print(plotobject) p.hours <- ggplot(dat, aes(x = Date, y = Hours)) + geom_line() + opts(title = "Hours Slept per Night") p.ave <- ggplot(dat2, aes(x = Days, y = Hours)) + geom_bar() + coord_polar() + opts(title = "Average Hours of Sleep per Day") p.th <- ggplot(dat, aes(x = Bedtime, y = Hours)) + geom_point() + opts(title = "Bedtime and Hours Slept") ## Put all plots in one big one pushViewport(vpList( viewport(x = 0, y = 0.5, width = .5, height = .5, just = c("left", "bottom"), name = "a"), viewport(x = .5, y = .5, width = .5, height = .5, just = c("left", "bottom"), name = "b"), viewport(x = 0, y = 0, width = 1, height = .5, just = c("left", "bottom"), name = "c"))) upViewport() downViewport("a") print(p.ave, newpage = FALSE) upViewport() downViewport("b") print(p.th, newpage = FALSE) upViewport() downViewport("c") print(p.hours, newpage = FALSE) On Fri, Jul 1, 2011 at 3:20 PM, Peter <smeldrick at gmail.com> wrote:> Hi - beginning R user question here - each day, over the course of several > months, I've tracked the time I go to bed, the time I wake up, and my hours > spent sleeping. ?What would be a good way to display this information? ?I > think it would be ideal to show something resembling a bar and whisker graph > for each day that would show the interval of hours spent asleep (or perhaps > just a bar "floating" against a backdrop showing the hours of a given > day/night), and that would also have a simple line graph of the total number > of hours per day. > > Can I get a hint perhaps? ?Thanks very much. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/