Skip to content

Instantly share code, notes, and snippets.

@infotroph
Created March 24, 2016 20:07
Show Gist options
  • Save infotroph/c288c39e260548c9d34c to your computer and use it in GitHub Desktop.
Save infotroph/c288c39e260548c9d34c to your computer and use it in GitHub Desktop.
A possibly terrible approach to plotting across year boundaries
library(ggplot2)
library(dplyr)
library(lubridate)
dates = c(
seq(from=as.Date("2014-10-01"), to=as.Date("2015-05-30"), by="days"),
seq(from=as.Date("2015-10-01"), to=as.Date("2016-05-30"), by="days"))
Tmin = rnorm(length(dates), mean=10*sin((yday(dates) - 90)/365*2*pi))
Tmax = Tmin + rnorm(length(dates), mean=5)
df = data.frame(dates, Tmin, Tmax)
df$winter =ifelse(df$dates < as.Date("2015-08-01"), "ONE", "TWO")
df$Tmin[df$dates > as.Date("2016-03-15")] = df$Tmin[df$dates > as.Date("2016-03-15")] - 5
df2 = (df
%>% mutate(dates= as.Date(dates))
%>% subset(month(dates) %in% c(1:5, 10:12))
%>% arrange(dates)
%>% group_by(winter)
%>% mutate(winterday = dates - dates[[1]], month=month(dates, label=TRUE))
%>% group_by(month)
%>% mutate(breakday=min(winterday)))
month_labels = unique(df2[,c("breakday", "month") ])
month_labels$breakday = as.numeric(month_labels$breakday)
(ggplot(df2, aes(x=winterday, ymin=Tmin, ymax=Tmax, fill=winter))
+geom_ribbon(alpha=0.5)
+scale_x_continuous(breaks=month_labels$breakday, labels=month_labels$month))
@infotroph
Copy link
Author

screen shot 2016-03-24 at 3 08 09 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment