tags:

The following image appeared on the Gnip blog last week. It compares tweet containing “SXSW” since 2007. For comparing timing over the years it is useful to align the plots by year, but let the y-scales float. Otherwise it isn’t possible to see the features in the early years due to the growth of Twitter.

The tricks here are pretty straight forward,

• Set scales=”free_y” in facet_wrap
• Create a dummy year, doy (the one below is for 2000, a year not shown)
• Label each year with a dummy scale using scale_x_date with custom breaks
• Give metric abbreviated y-labels (see format_si function)
• Add space between the plots as the date labels can be misleading when the plots have standard spacing (use panel.margin)

While the x-labels are correct, I don’t really like the look of how Jan 1 of the next year in each plot is hanging off to the right unlabeled.

```&lt;/pre&gt;
&lt;pre&gt;#!/usr/bin/env Rscript
library(ggplot2)
library(stringr)
library(gridExtra)
library(scales)

args &lt;- commandArgs(trailingOnly = TRUE)

format_si &lt;- function(...) {
# Format a vector of numeric values according
# to the International System of Units.
# http://en.wikipedia.org/wiki/SI_prefix
#
# Based on code by Ben Tupper
# https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html
# Args:
#   ...: Args passed to format()
#
# Returns:
#   A function to format a vector of strings using
#   SI prefix notation
#
# Usage:
#   scale_y_continuous(labels=format_si()) +
#
function(x) {
limits &lt;- c(1e-24, 1e-21, 1e-18, 1e-15, 1e-12,
1e-9,  1e-6,  1e-3,  1e0,   1e3,
1e6,   1e9,   1e12,  1e15,  1e18,
1e21,  1e24)
prefix &lt;- c(&quot;y&quot;,   &quot;z&quot;,   &quot;a&quot;,   &quot;f&quot;,   &quot;p&quot;,
&quot;n&quot;,   &quot;µ&quot;,   &quot;m&quot;,   &quot; &quot;,   &quot;k&quot;,
&quot;M&quot;,   &quot;G&quot;,   &quot;T&quot;,   &quot;P&quot;,   &quot;E&quot;,
&quot;Z&quot;,   &quot;Y&quot;)

# Vector with array indices according to position in intervals
i &lt;- findInterval(abs(x), limits)

# Set prefix to &quot; &quot; for very small values &lt; 1e-24
i &lt;- ifelse(i==0, which(limits == 1e0), i)

paste(format(round(x/limits[i], 1),
trim=TRUE, scientific=FALSE, ...),
prefix[i])
}
}

Y\$date &lt;- as.POSIXct(Y\$time)

png(filename = paste(sep=&quot;&quot;, args[1], &quot;.png&quot;), width = 550, height = 300, units = 'px')
print(
ggplot(data=Y) +
geom_line(aes(date, count), color=&quot;#e56d25&quot;) +
scale_y_continuous(labels=format_si()) +
scale_x_datetime(limits=c(as.POSIXct(&quot;2007-01-01&quot;), as.POSIXct(&quot;2012-09-01&quot;))) +
xlab(&quot;Date&quot;) +
ylab(&quot;Tweets per Day&quot;) +
ggtitle(args[2]) +
opts(legend.position = 'none',
panel.background = theme_rect(fill = &quot;#545454&quot;),
panel.grid.major = theme_line(colour = &quot;#757575&quot;),
panel.grid.minor = theme_line(colour = &quot;#757575&quot;)
)
)
dev.off()

##
# year over year comparison with facet wrap
#
# simulate dates in single year (2000 in this case),
# but give them yr factors for facet
# use custom formatting
#

Y\$Yr &lt;- as.factor(as.POSIXlt(Y\$time)\$year + 1900)
Y\$Mn &lt;- as.factor(1 + as.POSIXlt(Y\$time)\$mon)
Y\$Dy &lt;- as.factor(as.POSIXlt(Y\$time)\$mday)
# use dates for easier plotting
Y &lt;- transform(Y, doy = as.Date(paste(&quot;2000&quot;, Y\$Mn, Y\$Dy, sep=&quot;/&quot;)))

png(filename = paste(sep=&quot;&quot;, args[1], &quot;.year.png&quot;), width = 550, height = 800, units = 'px')
print(
ggplot(data=Y) +
geom_line(aes(doy, count), color=&quot;#e56d25&quot;) +
facet_wrap( ~ Yr, ncol = 1, scales=&quot;free_y&quot; ) +
scale_y_continuous(labels=format_si()) +
scale_x_date(labels=date_format(&quot;%b&quot;), breaks = seq(min(Y\$doy),max(Y\$doy),&quot;month&quot;)) +
xlab(&quot;Date&quot;) + ylab(&quot;Tweets per Day&quot;) +
labs( title = args[2] ) +
theme( legend.position = 'none',
panel.margin = unit(1.5, 'line'),
strip.text.x = element_text(size=12, face=&quot;bold&quot;),
panel.background = element_rect(fill = &quot;#545454&quot;),
panel.grid.major = element_line(colour = &quot;#757575&quot;),
panel.grid.minor = element_line(colour = &quot;#757575&quot;)
)
)
dev.off()&lt;/pre&gt;
```