Prototype of a horizon graph implementation based on ggplot2.
The idea came up by Saito and Heer. See 2009's CHI paper Sizing the Horizon: The Effects of Chart Size and Layering on the Graphical Perception of Time Series Visualizations
by Heer, Kong and Agrawala as well as 2005's Infovis paper Two-Tone Pseudo-Coloring: Compact Visualization for One-Dimensional Data. Proc.
by Saito et al.
A smoothed horizon graph of the first 200 days of the EuStockMarkets
dataset:
For the independent-geom-horizon.r
file, use a data.frame
of your liking and call plot_horizon
. The parameters are as followed:
data
: only supportsdata.frame
's. The order of the y-axis depends on the levels of the factorgroup
. Requiredmapping
: requires at leastx
,y
andgroup
. Requirednum.bands
: number of bandssmoothing
: applies the smoothing function. Only valid values areloess
andsplines
as charactersband.colors
: custom colours for the bands. Requires twice as many colours thannum.bands
. E.g. for num.bands=2 you would provide c(DarkNegative, BrightNegative, DarkPositive, BrightPositive)calculate.diff
: uses the percental difference between x and x+1 for the y-axisloess.span
: parameterspan
ofloess()
. Only applicable ifloess
is used for smoothingloess.interval
: parameter interval ofloess()
. Only applicable ifloess
is used for smoothingspline.n
: parametern
ofspline()
. Only applicable ifspline
is used for smoothing
plot_horizon
returns a ggplot plot.
df = data.frame(group="A", x=0:9, y=c(0.2,0.8627684,0.92,-1,-0.8324571,-1.0061331,-0.5056517,0.1085939,0.6393061,-0.9098858))
plot_horizon(df,aes(x,y,group=group),2,smoothing="spline", spline.n=40)
df = data.frame(group=factor(rep(c("A","B"),each=10)), x=0:9, y=c(0.8, 0.4627684, 0.2072174, -1, -0.8324571, -1.0061331, -0.5056517, 0.3085939, 0.4383061, -0.9098858, 0.3, 0.1627684, 0.3072174, -0.3, -1.8324571, -1.0061331, -0.5056517, 0.1085939, 0.6393061, -0.9098858))
plot_horizon(df,aes(x,y,group=group),2, smoothing="spline", spline.n=40)
eu()
is a function melting and adjusting the EuStockMarkets
dataset to be used with ggplot2. Using it with plot_horizon
:
plot_horizon(with(eu(),eu()[x <= 200,]), aes(x,y,group=group), num.steps=2, smoothing="loess", loess.span=0.2, loess.interval=0.1, calculate.diff=TRUE)
plot_horizon(with(eu(),eu()[x <= 200,]), aes(x,y,group=group), num.steps=2, smoothing="loess", loess.span=0.2, loess.interval=0.1)
Making a custom geom
out of the standalone function proves difficult since changing the y-axis labels from within a geom
is not allowed. Script geom-horizon.r
provides the first draft of the custom geom
.