Animation for transition from linear to logarithmic axis

Share:

Description

draw images that gradually transform from a linear to a logarithmic axis

Usage

1
2
3
4
5
linLogTrans(x, y, log = "x", steps = 100, base = 1, las = 1,
  plot = TRUE, xlim = range(x, finite = TRUE), ylim = range(y, finite =
  TRUE), box = TRUE, parexpr, endexpr, sleep = 0, firstplot = TRUE,
  lastplot = TRUE, write_t = TRUE, values_t = NULL, pointsarg = NULL,
  ...)

Arguments

x

x values to be plotted in animation

y

Vector with corresponding y values

log

Which axis is logarithmic, "x" or "y". DEFAULT: "x"

steps

Number of steps (images) in transition (About 30% are taken out). DEFAULT: 100

base

Base passed to logVals. DEFAULT: 1

las

par LabelAxisStyle (numbers upright). DEFAULT: 1

plot

Plot animations at all? False to just get the t-vector (used in linLogHist). DEFAULT: TRUE

xlim

xlim range in non-log units. DEFAULT: range(x, finite=TRUE)

ylim

ylim range in non-log units. DEFAULT: range(y, finite=TRUE)

box

Draw box at the end to overplot ablines crossing the box? DEFAULT: TRUE

parexpr

Characterized Expression to set par, eg. parexpr='par(mar=c(2,0.5,1.5,0.5), mpg=c(1.8,1,0))'

endexpr

Characterized Expression executed at the end of the plot, eg. endexpr='mtext("Probability density", line=-1, adj=0.03, outer=T)'

sleep

Pause time between frames, in seconds, passed to Sys.sleep. DEFAULT: 0

firstplot

Plot data on linear axis as additional first image? DEFAULT: TRUE

lastplot

Plot data on logarithmic axis as additional last image? DEFAULT: TRUE

write_t

Write transformation value in lower right corner? DEFAULT: TRUE

values_t

Supply vector with values for transformation (1/t). Overides steps. If you have a better algorithm than I do, please let me know! DEFAULT: NULL for internal calculation based on size of steps.

pointsarg

List of further arguments passed to points, like pch, cex, col. DEFAULT: NULL

...

Further arguments passed only to plot, like main, xlim, ylab. Excluded: x, y, las, xaxt, type

Value

Returned invisibly: transformation values used. Plotted: steps number of images.

Note

if(steps>1000) steps <- 1000. In the unlikely case you need more steps, please let me know and I'll change the code.
It's best to save the plots into a pdf (see the example) or wrap it within
png("Transition%03d"); linLogTrans(x,y); dev.off()

Author(s)

Berry Boessenkool, berry-b@gmx.de, June 2014

References

x^(1/t) is based on the first comment on http://stackoverflow.com/questions/15994442/
besides the nice graphic properties of logtransformations, check this page for the implications on rates of change:
http://sfew.websitetoolbox.com/post/show_single_post?pid=1282690259&postcount=4
http://sfew.websitetoolbox.com/post/show_single_post?pid=1282691799&postcount=5

See Also

logVals

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
set.seed(42);  x <- 10^rnorm(100, 3);  y <- runif(100)
linLogTrans(x,y, steps=15, sleep=0.01) # 0.05 might be smoother...
linLogTrans(x,y, steps=15, log="y", ylim=c(0.1, 0.8), base=c(1,2,5))

## Not run: 
## Rcmd check --as-cran doesn't like to open external devices such as pdf,
## so this example is excluded from running in the checks.
pdf("LinLogTransitionAnimation.pdf")
linLogTrans(x,y, main="Example Transition")
dev.off()

# if you have FFmpeg installed, you can use the animation package like this:
library2(animation)
saveVideo(linLogTrans(x,y, steps=300), video.name="linlog_anim.mp4", interval=0.01,
    ffmpeg="C:/ffmpeg-20150424-git-cd69c0e-win64-static/bin/ffmpeg.exe")


# old t values were dependent on the value of steps
findt <- function(steps) {
  # t-values for x^(1/t):
  allt <- 10^(seq(0,2.5,len=1e4) )
  # selection at upper half of these values;
  # Otherwise, the animation slows down too much at the end
  f <- 1.4 # multiplication factor due to length loss by unique
  sel <- round(seq(1, 10, len=f*steps)^4)   #0.5*seq(1, 100, len=1.3*steps)^2 + 0.5*
  sel2 <- unique(round(log10(seq(1, 10, len=f*steps))*f*steps))
  sel2[1] <- 1
  sel <- sel[sel2]
  # final t-values for transition:
  allt <- unique(round(allt[sel], 2))
  data.frame(x=seq(1,1000,len=length(allt)), t=allt)
  }

plot(findt(1000), type="l", log="y", las=1)
for(i in 5:999) lines(findt(i), col=rainbow2(1000)[i])
d <- findt(300)
lines(d) # good average

plot(d$x[-1], diff(d$t), type="l", ylim=c(3e-3,10), yaxt="n", log="y", main="t value growth rate")
logAxis(2) ; lines(d$x[-1], diff(d$t))
d2 <- findt(1000)
lines(d2$x[-1], diff(d2$t), col=2)
lines(2:1000, diff(linLogTrans(1,1, steps=1000, plot=F)), col=4)


d <- findt(300)
pdf("degreepoly.pdf")
for(i in 5:30)
  {
  plot(d, log="y", type="l", lwd=3, main=i, xlim=c(0,300), ylim=c(1,2))
  modell <- lm(t ~  poly(x,i, raw=T), data=d)
  lines(x2, predict(modell, data.frame(x=1:1300)), col=2)
  }
dev.off()   # 17 is good

cf <- coef(lm(t ~  poly(x,17, raw=T), data=d)) # these are currently used in the function
x <- 1:1000
y <- rowSums(sapply(1:18, function(i) cf[i]*x^(i-1)), na.rm=TRUE)
lines(x, y, lwd=3)
y[1] <- 1
plot(x, round(y, 3), ylim=c(1,3), xlim=c(0,500), type="l", log="")
dput(round(y, 3))

findn <- function(steps) nrow(findt(steps))
plot(1:1000, sapply(1:1000, findn), type="l")
abline(b=1, a=0)


## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.