Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Created March 4, 2015 23:18
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 TonyLadson/8281c5c619765634ed7d to your computer and use it in GitHub Desktop.
Save TonyLadson/8281c5c619765634ed7d to your computer and use it in GitHub Desktop.
Map missing values using a tile plot
# Usage
# MissingMap(df, date.column, var.column)
# df = data.frame that contains a column of dates and column of variables that may have missing values
# date.column = quoted name of the column cotaining dates
# var.column = quoted name of the column containing the variable
#
# Produces a tile plot showing here missing values occur in the sequence of dates
# https://tonyladson.wordpress.com/2014/12/21/mapping-missing-data/
MissingMap <- function(df, date.column, var.column){
require(lubridate)
require(ggplot2)
# labels for x axis
month.start <- yday(seq(as.Date('2010-01-01'),as.Date('2010-12-31'), by='months'))
# tile values
missing.factor <- factor(is.na(df[[var.column]]), labels = c('not missing','missing'))
# assemble data frame to pass to ggplot
df <- data.frame(x.day = yday(df[[date.column]]),
y.year = factor(year(df[[date.column]])),
var = df[[var.column]], missing.factor = missing.factor)
pl<- ggplot(data=df, aes(x.day, y.year))
pl + geom_tile(aes(fill = missing.factor)) +
scale_fill_manual(values = c('gray','red'), name='' ) +
theme_bw() +
theme(axis.title.x = element_text(colour="grey20", size=20, vjust=-2)) +
theme(axis.text.x = element_text(colour="grey20",size=12)) +
theme(axis.title.y = element_text(colour="grey20",size=20, vjust= 1)) +
theme(axis.text.y = element_text(colour="grey20",size=12)) +
theme(legend.title = element_text(colour="grey20",size=12)) +
theme(plot.margin = unit(c(2.5, 2.5, 2.5, 2.5), "cm")) +
ylab("Year") +
scale_x_continuous(name="Month", breaks=month.start, labels=month.abb)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment