Simple function animation - r

I want to generate a short and easy animation to illustrate how the function x^a changes for x between 0 and 1 and a increasing from 0 to 1. I run this code:
p1 <- seq(0, 1, 0.001)
alpha <- seq(0, 1, 0.05)
n <- length(alpha)
for (i in 1:n) {
p2 <- p1^(alpha[i])
p <- plot(p1, p2, ylim = c(0,1.1))
}
The problem is that R waits til the whole loop is done and then just displays all plots, so I have to skip through the plots by myself. What I rather want is that the newest plot replaces the old one and by this getting an animation. I tried using print()somehow, but it did not work. Is there any way to do an animation that way?
I know that there are animation packages, but they all seem more complicated than my way. However, if you think it would be better to use one of these, please tell.

Thanks for the help.
The adding of Sys.sleep() did exactly what I wanted to achieve.

Related

How to cumulate/preserve old frames using the Animation package?

The code below plots 10 dots on the x-axis, one at a time. At any one point in time, the animation displays only one dot. I am looking to end up with a plot with all 10 dots. I would like the second dot to be added to the plot showing the first dot; the third to be added to the plot showing the first two dots, etc. I looked at the documentation for saveGIF for a parameter that would somehow cumulate the frames, but could not find a relevant parameter there. It looks like it should be possible. Yihui Xie, the creator of the animation package, did exactly that in the top right plot of this example https://yihui.org/animation/example/buffon-needle/
library(animation)
saveGIF({
for (i in 1:10) {
x <- rnorm(1,0,1)
plot(x,0,xlim=c(0,1),ylim=c(-1,1))
}
})
You can generate the 10 numbers in advance, and plot them incrementally, e.g.,
library(animation)
saveGIF({
n <- 10
x <- rnorm(n, 0, 1)
for (i in seq_len(n)) {
plot(head(x, i), 0, xlim = c(1, n), ylim = c(-1, 1))
}
})

Automatically update graph title with parameter

I am not very familiar with R. I was using R to make the poisson distribution plot for different lambda (from 1 to 10), and display the plot for each just as a comparison.
But I would like to add a title say: "lambda = 1" for plot 1, "lambda=2" for plot 2 ... etc on the graph automatically according to lambda. But I wasn't able to figure out how to update the title automatically. This is my code, I was able to output 10 different graph correctly , but not sure how to update or add the corresponding lambda to the title automatically. Could someone give me some hint.
Also is it possible to say have a font size of "small" for the plot 1 to 5, and then a font size of 6 to 10?
Thanks
the_data_frame<-data.frame(matrix(ncol=10,nrow=21))
lam<-seq(1,10,1)
lam
x<-seq(0,20,1)
x
for (i in 1:10){
the_data_frame[i]<-exp(-lam[i])*lam[i]**x/gamma(x+1)
}
the_data_frame<-cbind(the_data_frame, x)
par(mfrow=c(5,2))
for (i in 1:10){
plot(the_data_frame[[i]]~the_data_frame[[11]], the_data_frame)
}
You can simplify the problem. Using one loop, over the lamda values, you compute at each iteration the value of y using the poison formula then you plot it. I use main argument to add a title for each plot. Here I am using bquote to get a plotmath format of lambda value.
For example , for 4 values of lambda , you get:
x<-seq(0,20,1);lam = c(0.5,1,2,4)
par(mfrow=c(2,2))
lapply(lam,function(lamd){
y <- exp(-lamd)*lamd*x/gamma(x+1)
plot(x,y,main=bquote(paste(lambda,'=',.(lamd))),type='l')
})
This might help:
for (i in 1:10){
plot(the_data_frame[[i]]~the_data_frame[[11]], the_data_frame,
main=paste("lambda=", i, sep=""))
}
library(ggplot2)
xval <- rep(0:20,10)
lambda <- rep(1:10,21)
yvtal <- exp(- lambda)*lambda**xval/gamma(xval+1)
the_new_data_frame <- data.frame(cbind(xval,lambda,yval))
plot1 <- ggplot(the_new_data_frame, aes(xval, yval)) + geom_line(aes(colour=factor(lambda)))
plot1
plot1 + facet_grid(~lambda)
Were you looking for an interactive window where you can input the text and update the figure title? If yes you may want to look for the tcltk package.
See
http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/modalDialog.html

function lines() is not working

I have a problem with the function lines.
this is what I have written so far:
model.ew<-lm(Empl~Wage)
summary(model.ew)
plot(Empl,Wage)
mean<-1:500
lw<-1:500
up<-1:500
for(i in 1:500){
mean[i]<-predict(model.ew,data.frame(Wage=i*100),interval="confidence",level=0.90)[1]
lw[i]<-predict(model.ew,data.frame(Wage=i*100),interval="confidence",level=0.90)[2]
up[i]<-predict(model.ew,data.frame(Wage=i*100),interval="confidence",level=0.90)[3]
}
plot(Wage,Empl)
lines(mean,type="l",col="red")
lines(up,type="l",col="blue")
lines(lw,type="l",col="blue")
my problem i s that no line appears on my plot and I cannot figure out why.
Can somebody help me?
You really need to read some introductory manuals for R. Go to this page, and select one that illustrates using R for linear regression: http://cran.r-project.org/other-docs.html
First we need to make some data:
set.seed(42)
Wage <- rnorm(100, 50)
Empl <- Wage + rnorm(100, 0)
Now we run your regression and plot the lines:
model.ew <- lm(Empl~Wage)
summary(model.ew)
plot(Empl~Wage) # Note. You had the axes flipped here
Your first problem was that you flipped the axes. The dependent variable (Empl) goes on the vertical axis. That is the main reason you didn't get any lines on the plot. To get the prediction lines requires no loops at all and only a single plot call using matlines():
xval <- seq(min(Wage), max(Wage), length.out=101)
conf <- predict(model.ew, data.frame(Wage=xval),
interval="confidence", level=.90)
matlines(xval, conf, col=c("red", "blue", "blue"))
That's all there is to it.

Reinitializing variables in R and having them update globally

I'm not sure how to pose this question with the right lingo and the related questions weren't about the same thing. I wanted to plot a function and noticed that R wasn't udpating the plot with my change in a coefficient.
a <- 2
x <- seq(-1, 1, by=0.1)
y <- 1/(1+exp(-a*x))
plot(x,y)
a <- 4
plot(x,y) # no change
y <- 1/(1+exp(-a*x)) # redefine function
plot(x,y) # now it updates
Just in case I didn't know what I was doing, I followed the syntax on this R basic plotting tutorial. The only difference was the use of = instead of <- for assignment of y = 1/(1+exp(-a*x)). The result was the same.
I've actually never just plotted a function with R, so this was the first time I experienced this. It makes me wonder if I've seen bad results in other areas if re-defined variables aren't propagated to functions or objects initialized with the initial value.
1) Am I doing something wrong and there is a way to have variables sort of dynamically assigned so that functions take into account the current value vs. the value it had when they were created?
2) If not, is there a common way R programmers work around this when tweaking variable assignments and making sure everything else is properly updated?
You are not, in fact, plotting a function. Instead, you are plotting two vectors. Since you haven't updated the values of the vector before calling the next plot, you get two identical plots.
To plot a function directly, you need to use the curve() function:
f <- function(x, a)1/(1+exp(-a*x))
Plot:
curve(f(x, 1), -1, 1, 100)
curve(f(x, 4), -1, 1, 100)
R is not Excel, or MathCAD, or any other application that might lead you to believe that changing an object's value might update other vectors that might have have used that value at some time in the past. When you did this
a <- 4
plot(x,y) # no change
There was no change in 'x' or 'y'.
Try this:
curve( 1/(1+exp(-a*x)) )
a <- 10
curve( 1/(1+exp(-a*x)) )

Plotting during a loop in RStudio

I am implementing a solution to the Traveling Salesman Problem (TSP) in R (simulated Annealing) and I want to output the current best path periodically. I have searched quite a bit for how to output plots during a for loop and have thus far failed.
I use RStudio, and want to see the graphs as they are generated. If you have ever watched TSP solvers do their thing, you will understand how cool it is to watch. Here is a sample of the graphics output I want to see http://www.staff.science.uu.nl/~beuke106/anneal/anneal.html
I don't think that the memory usage will be a problem (during about 500,000 iterations, I am only expecting 50-100 plots). Here is a sample function, where we would expect to see 10 different plots during the time the function runs:
Plotz <- function(iter = 1000000, interval = 100000) {
x <- 1:10
for(i in 1:iter){
y <- runif(10)
if(i %% interval == 0) {
plot(x, y)
}
}
return(c(x, y))
}
Plotz()
When I run this, all I see is the final plot produced (in RStudio). How can I see the plots as they're generated?
Also: I am on Ubuntu (whatever the newest stable release is). Don't know if that is relevant.
Thank you everyone in advance.
EDIT: Per Captain Murphy's suggestion, I tried running this in the Linux terminal, and the graphics appeared. I still think the question of "How to do this in RStudio?" Is still relevant, however. It's such a good program, so maybe someone has an idea of what could be done to get this to work?
EDIT2: As Thilo stated, this is a known bug in Rstudio. If anyone has any other ideas to solve this without the software itself being fixed, then there is still something to discuss. Otherwise, consider this question solved.
Calling Sys.sleep(0) should cause the plot to draw. Unlike the X11 solution, this will work on server versions of RStudio as well.
(I was surprised that dev.flush() did not give the result you were hoping for, that might be a bug.)
One thing you can do is open a x11 window and plot in there:
x11()
Plotz()
That should work the same as running it in terminal.
Following up on #JoeCheng's answer and #RGuy's comment on that answer: as I worked out with the RStudio folks, the problem seems to primarily arise when there is too much plotting going on in too short a timespan. The solution is twofold:
Sys.sleep(0) helps force an update to the plotting window.
Plotting updates every Wth loop rather than every loop.
For instance, on my computer (i7, RStudio Server), the following code does not update until the loop completes:
N <- 1000
x <- rep(NA,N)
plot(c(0,1)~c(0,N), col=NA)
for(i in seq(N)) {
Sys.sleep(.01)
x[i] <- runif(1)
iseq <- seq(i-99,i)
points( x[i]~i )
Sys.sleep(0)
}
The following code updates in real-time, despite having the same number of points to be plotted:
N <- 1000
x <- rep(NA,N)
plot(c(0,1)~c(0,N), col=NA)
for(i in seq(N)) {
Sys.sleep(.01)
x[i] <- runif(1)
iseq <- seq(i-99,i)
if(i%%100==0) {
points( x[iseq]~iseq )
Sys.sleep(0)
}
}
In other words, it's the number of calls the plot that seems to matter, not the amount of data to be plotted.
If you want to save the plots as well you could just open a new device in the loop and close it afterwards.
Plotz <- function(iter = 1000, interval = 100) {
x <- 1:10
p <- 0 #plot number
for(i in 1:iter){
y <- runif(10)
if(i %% interval == 0) {
png(file=paste(i,"png",sep="."))
p <- p + 1; plot(x, y)
dev.off()
}
}
return(c(x, y))
}
Plotz <- function(iter = 1000, interval = 100) {
x <- 1:10
p <- 0 #plot number
for(i in 1:iter){
y <- runif(10)
if(i %% interval == 0) {
p <- p + 1; plot(x, y)
readline("Please press the Enter key to see the next plot if there is one.")
}
}
return(c(x, y))
}
Plotz()
You can also use the back arrows on the plots tab of the lower left pane of the RStudio interface in order to view the plots.
You can use the animate package to layer your plots into a GIF.

Resources