Skip to content

Instantly share code, notes, and snippets.

@flashton2003
Created March 26, 2016 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flashton2003/b590d81389988f6b2d55 to your computer and use it in GitHub Desktop.
Save flashton2003/b590d81389988f6b2d55 to your computer and use it in GitHub Desktop.
library(reshape2)
library(ggplot2)
#library(scales)
# from http://stackoverflow.com/questions/3550341/gantt-charts-with-r
books <- c("All the Light We Cannot See", "13 Things That Don t Make Sense", "Why The Allies Won", "The Third Policeman", "Just Kids", "Hackers", "The Black Swan", "Prisoners of Geography", "Benjamin Franklin - Biography", "Stuff Matters", "Pale Fire", "Use Of Weapons", "Lustrum", "Microbe Hunters", "Perfume", "Winston s War", "Being Mortal", "The Man Who Mistook His Wife for a Hat", "The Life You Can Save", "The Ghost Map", "Hyperion", "Chaos - Making a New Science", "The Realm", "A Scientist in Wonderland", "Good Omens", "White Teeth", "The Sports Gene", "The Inimitable Jeeves", "The Illustrated Man", "How We Got to Now", "Gone Girl", "A Dance With Dragons", "H is for Hawk", "Green Mars", "Pompeii - Life of a Roman Town", "The Grapes of Wrath", "The Forest Unseen", "The Psychopath Test", "The Selfish Gene", "The Difference Engine", "Managing Your Boss", "Empire - How Britain Made the Modern World", "The Gun Seller", "The Magic of Recluce", "Little Brother", "Alistair Cooke s America", "The Information", "The Double Helix", "I Am The Secret Footballer", "Steve Jobs - Biography", "The Martian", "Autobiography - Morrisey", "Red Mars", "Ender s Game")
dfr <- data.frame(
name = factor(books, levels = books),
start.date = as.Date(c("2016-02-07", "2016-01-31", "2016-01-07", "2016-01-03", "2015-12-28", "2015-12-15", "2015-11-24", "2015-11-17", "2015-10-29", "2015-10-19", "2015-09-14", "2015-09-06", "2015-08-31", "2015-08-16", "2015-08-15", "2015-08-10", "2015-07-27", "2015-07-15", "2015-06-11", "2015-06-03", "2015-05-18", "2015-04-12", "2015-04-11", "2015-04-07", "2015-03-29", "2015-03-06", "2015-03-01", "2015-02-27", "2015-02-21", "2015-02-16", "2015-02-07", "2015-01-10", "2015-01-09", "2014-12-18", "2014-12-08", "2014-11-17", "2014-10-30", "2014-10-27", "2014-09-21", "2014-09-01", "2014-08-31", "2014-08-18", "2014-08-16", "2014-08-12", "2014-08-07", "2014-08-01", "2014-07-08", "2014-07-06", "2014-07-05", "2014-06-22", "2014-06-03", "2014-02-19", "2014-02-07", "2014-02-01")),
end.date = as.Date(c("2016-02-23", "2016-02-07", "2016-01-31", "2016-01-07", "2016-01-03", "2015-12-28", "2015-12-15", "2015-11-24", "2015-11-17", "2015-10-29", "2015-10-19", "2015-09-14", "2015-09-06", "2015-08-31", "2015-08-16", "2015-08-15", "2015-08-10", "2015-07-27", "2015-07-15", "2015-06-11", "2015-06-03", "2015-05-18", "2015-04-12", "2015-04-11", "2015-04-07", "2015-03-29", "2015-03-06", "2015-03-01", "2015-02-27", "2015-02-21", "2015-02-16", "2015-02-07", "2015-01-10", "2015-01-09", "2014-12-18", "2014-12-08", "2014-11-17", "2014-10-30", "2014-10-27", "2014-09-15", "2014-09-01", "2014-08-31", "2014-08-18", "2014-08-16", "2014-08-12", "2014-08-07", "2014-08-01", "2014-07-08", "2014-07-06", "2014-07-05", "2014-06-22", "2014-06-03", "2014-02-19", "2014-02-07")),
is.finished = c(TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE),
pages = c(544, 256, 508, 205, 304, 520, 400, 256, 608, 272, 315, 396, 459, 363, 363, 704, 293, 281, 224, 316, 528, 352, 86, 174, 406, 480, 353, 247, 243, 256, 512, 592, 322, 641, 368, 469, 277, 290, 384, 384, 56, 448, 352, 510, 387, 400, 544, 226, 256, 592, 394, 400, 588, 336)
)
dfr$days_to_read <- as.numeric(difftime(dfr$end.date, dfr$start.date, units = c("days")))
dfr$pages_per_day <- dfr$pages / dfr$days_to_read
## do the books per month plot
ggplot(aes(x = dfr$start.date, fill = is.finished), data = dfr) +
geom_histogram(binwidth = 30) +
labs(x = "Month", y = "Number of books started") +
scale_x_date(labels = date_format("%b-%Y"), date_breaks = "3 months") +
theme(text = element_text(size = 20))
ggplot(aes(x = dfr$days_to_read, fill = is.finished), data = dfr) +
geom_histogram(binwidth = 2)
## change the shape of the data to set it up for the start-end plot
mdfr <- melt(dfr, measure.vars = c("start.date", "end.date"))
## do the start end plot
ggplot(mdfr, aes(value, name, colour = is.finished)) +
geom_line(size = 6) +
xlab(NULL) +
ylab(NULL) +
theme(text = element_text(size = 15)) +
scale_x_date(labels = date_format("%b-%Y"), date_breaks = "3 months")
# according to https://trinkerrstuff.wordpress.com/2013/08/14/how-do-i-re-arrange-ordering-a-plot-revisited/
dfr2 <- dfr
dfr2$name <-factor(dfr$name, levels = dfr[order(dfr$pages_per_day), "name"])
ggplot(dfr2, aes(x = name, y = pages_per_day, fill = is.finished)) +
geom_bar(stat = 'identity') +
#theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
xlab(NULL) +
labs(y = "Pages per Day") +
theme(text = element_text(size = 20)) +
coord_flip() +
scale_y_continuous(breaks=seq(0,350,50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment