john_w
john_w

Reputation: 701

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)
}

Upvotes: 0

Views: 1217

Answers (4)

JD002
JD002

Reputation: 113

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

Upvotes: 0

David
David

Reputation: 1

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)

Upvotes: 0

agstudy
agstudy

Reputation: 121568

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')

})

enter image description here

Upvotes: 1

Linens
Linens

Reputation: 7942

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=""))
}

Upvotes: 0

Related Questions