6

I have time series data of one month plotted day-wise as enter image description here

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.

Haroon Lone
  • 300
  • 1
  • 10

2 Answers2

7

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)

enter image description here

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)

enter image description here

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)

enter image description here

hrbrmstr
  • 351
  • 1
  • 10
3

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)

enter image description here

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)

enter image description here

Marmite Bomber
  • 1,113
  • 1
  • 8
  • 11