cranvas (version 0.8.5)

qtime: Draw a time plot

Description

Draw a time-series plot.

Usage

qtime(time, y, data, group = NULL, shift = c(1, 4, 7, 12, 24), alpha = 1, size = 2, asp = NULL, series.stats = ifelse(nrow(data) < 1000, TRUE, FALSE), fun.base = min, main = NULL, xlab = NULL, ylab = NULL, ...)

Arguments

time
the variable indicating time on the horizontal axis.
y
a vertor of all the variable names of interest.
data
Mutaframe data generated by qdata.
group
Similar to period, but is used for longitudinal data grouping.
shift
Wrapping speed selector. The default possible speeds are 1,7(for days a week),12(for months),24(for hours).
size
Point size, default to be 2.
alpha
Transparency level, 1=completely opaque, default to be 1.
asp
Ratio between width and height of the plot.
series.stats
Whether to show the statistics which measure the similarity between series when wrapping. It gives the ACF, corr, and R square for one, two, and more series respectively.
fun.base
a function to compute the baseline of the area plot
main
main title for the plot.
xlab
label on horizontal axis, default is name of x variable
ylab
label on vertical axis, default is name of y variable

Details

Arrow up/down: in-/de-crease size of points.

Arrow left/right: wrap the time series in the initial mode, and drag the series in the series selection mode.

Shift + right: Time series will be folded directly to the width of maximal value in argument shift.

Shift + left: Time series will be backed to the original xaxis position.

Key '+'/'-': de-/in-crease alpha level (starts at alpha=1 by default).

Key 'u'/'d': separate/mix the series groups or the period by shifting them up and down.

Shift + 'u'/'d': for multivariate y's, separate/mix them by shifting up and down.

Key 'g': change the wrapping period circularly in the values of parameter 'shift'.

Key 'm': Switch the mode for series selection. Default to be off. When there are more than one series in the plot, users can turn it on to hold a series and shift the series horizontally by dragging with the mouse.

Key 'y': y-wrapping

Shift + 'y': y-wrapping backward

Key 'r': switch to the area plot

Key 'f': fold the series to a horizon plot

Wheel: Zoom in/out. Then users can drag the series horizontally to see the details.

See Also

Other plots: qbar; qboxplot; qdensity; qhist, qspine; qmval; qparallel

Examples

Run this code
library(cranvas)

## example 1: NASA temperature data
nasa2221 <- subset(nasa, Gridx == 22 & Gridy == 21)
nasa2221$Year <- factor(nasa2221$Year)
qnasa <- qdata(nasa2221)

qtime(TimeIndx,ts,qnasa,shift=c(1,12))
qscatter(ts,ps_tovs,data=qnasa)

qtime("TimeIndx","ts",qnasa,Year,shift=c(1,12))
qtime(TimeIndx,c("ts","ca_med","ps_tovs"),qnasa,shift=c(1,12))
qtime("TimeIndx",c(ts,ca_med,ps_tovs),qnasa,Year)


##
nasa2221 <- subset(nasa, Gridx %in% c(14,17,20) & Gridy == 21)
#nasa2221 <- subset(nasa, Gridy == 21)
nasa2221$Year <- factor(nasa2221$Year)
nasa2221$Gridx <- factor(nasa2221$Gridx)
qnasa <- qdata(nasa2221)
qtime("TimeIndx",c(ts,ca_med,o3_tovs),qnasa,Gridx,shift=c(1,12))
qscatter(o3_tovs,ts,data=qnasa)
library(ggplot2)
nasa.locs <- subset(cranvas::nasa, TimeIndx == 1)
nasa.locs$loc <- paste(nasa.locs$Gridx, nasa.locs$Gridy, sep=",")
qplot(Long, Lat, data=nasa.locs, geom="text", label = loc)

## example 2: Remifentanil in the nlme package
library(nlme)
qRem <- qdata(Remifentanil[complete.cases(Remifentanil) & Remifentanil$ID==1,])
qtime(Time, conc, qRem)

Remi <- Remifentanil[complete.cases(Remifentanil),]
Remi$ID <- factor(Remi$ID)
qRemi <- qdata(Remi)
qtime(Time, conc, qRemi, group=ID)
qscatter(Amt, conc, data=qRemi)
# for categorical brushing self-link dataset by ID:
# id <- link_cat(qRemi, "ID")
# remove_link(qRemi, id)


## example 3: Wages
library(dplyr)
wages.num <- summarise(group_by(wages, id), n=length(lnw))
indx <- wages.num$id[wages.num$n > 11]
wages.sub <- subset(wages, id %in% indx)
nindiv <- length(unique(wages.sub$id))
wages.sub$idno <- factor(wages.sub$id, labels=1:nindiv)
wages.sub.demog <- summarise(group_by(wages.sub, idno), n=length(lnw),
                       avlnw = mean(lnw, na.rm=T),
                       #trendlnw = lsfit(exper, lnw)$coef[2],
                       trendlnw = max(lnw, na.rm=T) - min(lnw, na.rm=T),
                       black = black[1],
                       hispanic = hispanic[1],
                       ged = ged[1], hgc = hgc[1],
                       avunemp = mean(uerate, na.rm=T))
#qwage <- qdata(wages[as.integer(as.character(wages$id))<2000,1:3])
qwages <- qdata(wages.sub[,c(11,2:3)])
qtime(exper, lnw, qwages, group=idno)
# id <- link_cat(wage, "id")
# remove_link(wage, id)

indx <- wages.num$id[wages.num$n > 3]
wages.sub2 <- subset(wages, id %in% indx)
wages.sub2.demog <- summarise(group_by(wages.sub2, id), n=length(lnw),
                             avlnw = mean(lnw, na.rm=T),
                             #trendlnw = coefficients(lm(lnw ~ exper))[2],
                             rangelnw = max(lnw, na.rm=T) - min(lnw, na.rm=T),
                             sdlnw = sd(lnw, na.rm=T),
                             startlnw = lnw[1],
                             endlnw = lnw[length(lnw)],
                             inclnw = (lnw[length(lnw)]-lnw[1])/lnw[1]*100.0,
                             black = black[1],
                             hispanic = hispanic[1],
                             ged = ged[1], hgc = hgc[1],
                             avunemp = mean(uerate, na.rm=T))
qwages <- qdata(wages.sub2[,1:3])
qwages.demog <- qdata(wages.sub2.demog)
id = link_cat(qwages.demog, "id", qwages, "id")
qtime(exper, lnw, qwages, group=id)
qscatter(startlnw, inclnw, qwages.demog)
qbar(hgc, qwages.demog)
qhist(avunemp, qwages.demog)
remove_link(qwages.demog, id[1])
remove_link(qwages, id[2])
# Now need to link scatterplots of demographics with time series

## example 4: Lynx - for posterity
# Good to show off wrapping to investigate irregular series
qlynx <- qdata(data.frame(Time=1:114, lynx))
qtime(Time, lynx, qlynx, shift=1:13)


## example 5: Sunspots - for posterity
# Good to show off wrapping to investigate irregular series
qsun <- qdata(data.frame(Time=1:2820, sunspots))
qtime(Time, sunspots, qsun, shift=c(1,c(1,6,7,13,26)*10))


## example 6: Pigs
qpig <- qdata(pigs)
qtime(TIME, c("GILTS","PROFIT","PRODUCTION","HERDSZ"), qpig, shift=c(1,4))


## example 7: flu trends
flu.data <- read.table("http://www.google.org/flutrends/us/data.txt", skip=11, sep=",", header=TRUE)
# Get only states
flu.data <- flu.data[, c(1, 3:53)]
# Melt data, and rename variables
library(reshape)
flu.melt <- melt(flu.data, id.vars="Date")
flu.melt$Date <- as.Date(flu.melt$Date)
colnames(flu.melt)[2] <- "State"
colnames(flu.melt)[3] <- "FluSearches"
flu.melt$days <- as.vector(difftime(flu.melt$Date,as.Date('2002-12-31')))
summary(flu.melt$Date)
flu.melt$Date[flu.melt$days>2500&flu.melt$days<2520]
qflu <- qdata(flu.melt)
qtime(days, FluSearches, data=qflu, group="State",shift=c(1,7,28,364))
# winter of 2014
flu2014 <- subset(flu.melt, days>3960)
ord <- names(sort(tapply(flu2014$FluSearches,flu2014$State,function(x)which(x>(max(x)/5*3))[1])))
flu2014$State <- factor(flu2014$State,levels=ord)
qflu <- qdata(flu2014)
qtime(days, FluSearches, data=qflu, group="State",shift=c(1,7,28,35,91))

cranvas_off()

Run the code above in your browser using DataCamp Workspace