Skip to content

Instantly share code, notes, and snippets.

@AndrewTheTM
Created March 18, 2016 15:27
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 AndrewTheTM/2cce5d650e6a0c4af757 to your computer and use it in GitHub Desktop.
Save AndrewTheTM/2cce5d650e6a0c4af757 to your computer and use it in GitHub Desktop.
library(openxlsx)
library(ggplot2)
library(reshape2)
library(plyr)
# Fix the path!
cd = read.xlsx("countdata.xlsx", "Sheet1")
# Summarize counts to get average weekdays
cntByDay.1 = ddply(cd, .(DayOfWeek, CntTimeTxt), summarize, NB = mean(NB), SB = mean(SB))
# Reformat data to what ggplot2 wants
cntByDay = melt(cntByDay.1, id.vars = c("DayOfWeek", "CntTimeTxt"))
# We need some of these to be factors, and we need to reformat a little
cntByDay$CntTimeTxt = factor(cntByDay$CntTimeTxt)
cntByDay$DayTime = paste(cntByDay$DayOfWeek, cntByDay$variable, sep = " - ")
cntByDay$DayTime = factor(cntByDay$DayTime, levels= c("Monday - NB", "Monday - SB", "Tuesday - NB", "Tuesday - SB", "Wednesday - NB", "Wednesday - SB", "Thursday - NB", "Thursday - SB", "Friday - NB", "Friday - SB", "Saturday - NB", "Saturday - SB", "Sunday - NB", "Sunday - SB"))
# Plot the data
heatmap = ggplot(cntByDay, aes(x = DayTime, y = CntTimeTxt, fill = value, color = NULL))+
geom_bin2d()+xlab("Day and Direction") + ylab("Time of Day") +
ggtitle("Average Weekday Count") +
scale_fill_gradient(low="green", high="red") +
scale_y_discrete(limits = rev(levels(cntByDay$CntTimeTxt)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment