
Last chance! 50% off unlimited learning
Sale ends in
draw images that gradually transform from a linear to a logarithmic axis
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,
...)
x values to be plotted in animation
Vector with corresponding y values
Which axis is logarithmic, "x" or "y". DEFAULT: "x"
Number of steps (images) in transition (About 30% are taken out). DEFAULT: 100
Base passed to logVals
. DEFAULT: 1
par
LabelAxisStyle (numbers upright). DEFAULT: 1
Plot animations at all? False to just get the t-vector (used in linLogHist
). DEFAULT: TRUE
xlim range in non-log units. DEFAULT: range(x, finite=TRUE)
ylim range in non-log units. DEFAULT: range(y, finite=TRUE)
Draw box at the end to overplot ablines
crossing the box? DEFAULT: TRUE
Characterized Expression to set par
, eg. parexpr='par(mar=c(2,0.5,1.5,0.5), mpg=c(1.8,1,0))'
Characterized Expression executed at the end of the plot, eg.
endexpr='mtext("Probability density", line=-1, adj=0.03, outer=T)'
Pause time between frames, in seconds, passed to Sys.sleep
. DEFAULT: 0
Plot data on linear axis as additional first image? DEFAULT: TRUE
Plot data on logarithmic axis as additional last image? DEFAULT: TRUE
Write transformation value in lower right corner? DEFAULT: TRUE
Supply vector with values for transformation (1/t). Overrides steps. If you have a better algorithm than I do, please let me know! DEFAULT: NULL for internal calculation based on size of steps.
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
Returned invisibly: transformation values used. Plotted: steps
number of images.
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
# NOT RUN {
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)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab