# Represent time-series data in much compact form

I have time series data of one month plotted day-wise as

Notice every day follows a different pattern. Now, I want to show this "diversity in pattern" of each day in much compact form in a research paper. What are the different ways/options of representing this in compact form using R.

• Are you thinking visual representation, or mathematical? If it's the former, look at sparklines. If it's the latter, look at PCA; the PCA coefficients will give you a succinct summary.
– Emre
Commented Feb 8, 2016 at 6:05
• Thanks @Emre for pointing to sparklines, but sparklines still take enough space on paper,i.e, thirty rows (for each day). I want something more compact. Commented Feb 8, 2016 at 6:17
• How about 2D PCA? Each time series will be represented as a point on one chart.
– Emre
Commented Feb 8, 2016 at 7:40
• Sparklines is a good idea, maybe just comma-seperated next to each other (depending on what you wanna show) - just like flowing text, Or plot several lines in one chart - maybe 4 to 5 lines per 7 charts if you aggregate by weekday. But I guess you already tried that... Commented Feb 8, 2016 at 9:08
• Difficult to say but if you might have expected periodicity then Panel of 4 (and 1 of 3), with 7 colours for 7 days would be compact and show/not show the relation? You would also want to do something like ARIMA plot? Commented Feb 8, 2016 at 14:28

Simulate some data:

library(ggplot2)
library(purrr)
library(ggthemes)

days <- seq(as.Date("2015-08-01"), as.Date("2015-08-31"), by="1 day")
hours <- sprintf("%02d", 0:23)

map_df(days, function(x) {
map_df(hours, function(y) {
data.frame(day=x, hour=y, val=sample(2500, 1), stringsAsFactors=FALSE)
})
}) -> df


Check it:

ggplot(df, aes(x=hour, y=val, group=day)) +
geom_line() +
facet_wrap(~day) +
theme_tufte(base_family="Helvetica") +
labs(x=NULL, y=NULL)


Since you're only trying to convey the scope of the variation, perhaps use a boxplot of the values of hours across days?

ggplot(df, aes(x=hour, y=val)) +
geom_boxplot(fill="#2b2b2b", alpha=0.25, width=0.75, size=0.25) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_tufte(base_family="Helvetica") +
theme(axis.ticks=element_blank()) +
labs(x=NULL, y=NULL)


That can be tweaked to fit into most publication graphics slots and the boxplot shows just how varied each day's readings are.

You could also use boxplot.stats to get the summary data and plot it on a line chart:

library(dplyr)
library(tidyr)

bps <- function(x) {
cnf <- boxplot.stats(x)$conf data.frame(as.list(set_names(cnf, c("lwr", "upr"))), mean=mean(x)) } group_by(df, hour) %>% do(bps(.$val)) %>%
ggplot(aes(x=hour, y=mean, ymin=lwr, ymax=upr, group=1)) +
geom_ribbon(fill="#2b2b2b", alpha=0.25) +
geom_line(size=0.25) +
theme_tufte(base_family="Helvetica") +
theme(axis.ticks=element_blank()) +
labs(x=NULL, y=NULL)


Two alternative way are a density plot for each hour and level plot of hour and factorized value.

I'm showing ganarated data with two different unified distribution (for day and night hours)

Densityplot

library(lattice)
xy <- densityplot(~ value,  groups = hour ,
plot.points=FALSE,
data =  df,
scales=list(x=list(rot=90, cex= .9 ),y=list(cex=.9)),par.strip.text=list(cex=.8),
ylab="density", xlab="value", main=paste(  "DensityPlot"  ) )
print (xy)


Levelplot

library(RColorBrewer)
xy <- levelplot(cnt ~    hour + value,
col.regions=colorRampPalette(brewer.pal(9,"YlOrRd"))(16) ,
data =  df,
scales=list(x=list(rot=90, cex= .9 ),y=list(cex=.9)),par.strip.text=list(cex=.8),
ylab="value", xlab="hour", main=paste( "LevelPlot"  ) )
print (xy)