Re: [R] loop and plot
Dear all, I am stuck at applying loop function for creating separated plots. I have coding like below: dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b) )) kk = function(f) { ls=as.character(f) pie(dataset.table[ls,],main=ls) box() } kk(1) kk(2) kk(3) By using above code, I can create 3 single plot respectively, but when I type kk(1:3), obviously it will not work. I know I have to vectorise the coding, then I can use command kk(1:3). I try to use loop: kk = function(f) { ls=as.character(f) for (i in length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } kk(1:3) the above code only gives me the last pie plot (ie. kk(3) plot) instead of 3 plots respectively. Can someone please guide me how to revise the loop coding, and produce 3 separated plots one after another on the screen by typing kk(1:3)? Thanks a lot. Rene. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop and plot
Hi Rene, the problem is probably due to the fact that R will send all plots to the same graphical output window. Each next plot just replaces the previous one. if it's only a few plots, you can divide the graphical window with the commands par(mfrow=...) (see ?par) or layout(matrix(...)) (see ?layout). Otherwise you have to ask the window to wait before refreshing. par(ask=TRUE) (see ?par as well) Hope this helps Cheers Joris On Mon, Oct 19, 2009 at 12:14 PM, Rene kaixinma...@gmail.com wrote: Dear all, I am stuck at applying loop function for creating separated plots. I have coding like below: dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b) )) kk = function(f) { ls=as.character(f) pie(dataset.table[ls,],main=ls) box() } kk(1) kk(2) kk(3) By using above code, I can create 3 single plot respectively, but when I type kk(1:3), obviously it will not work. I know I have to vectorise the coding, then I can use command kk(1:3). I try to use loop: kk = function(f) { ls=as.character(f) for (i in length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } kk(1:3) the above code only gives me the last pie plot (ie. kk(3) plot) instead of 3 plots respectively. Can someone please guide me how to revise the loop coding, and produce 3 separated plots one after another on the screen by typing kk(1:3)? Thanks a lot. Rene. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop and plot
Hi Rene, two reasons : - you have to specify the par first in your function, before plotting. - you say for (i in length(f)), but length(f) is a vector with only one value. The correct syntax is : kk = function(f) { par(mfrow=c(1,length(f))) ls=as.character(f) for (i in 1:length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } There is another important problem : this function only works if dataset.table is specified in your environment. I would do something like this : dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b) )) kk = function(f,data=dataset.table) { par(mfrow=c(3,2)) ls=as.character(f) for (i in 1:length(f)) { pie(data[ls[i],],main=ls[i]) box() } } kk(1:3) newdata - table(data.frame(var1=c(1,2,3,1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b,c,b,a) )) kk(1:3,data=newdata) This allows you to use the function on other datasets, but you don't have to specify your default dataset each time. Cheers Joris On Mon, Oct 19, 2009 at 1:33 PM, Rene kaixinma...@gmail.com wrote: Thanks Joris for the prompt reply. I have tried to use par(mfrow=(3,3)), then I type kk(1:3), but it still ends up showing the last plot only instead of 3 plots. I think there must be sth wrong with my loop code, can you see where I did wrong? dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b) )) kk = function(f) { ls=as.character(f) for (i in length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } par(mfrow=(3,2)) kk(1:3) Thanks a lot! Rene -Original Message- From: joris meys [mailto:jorism...@gmail.com] Sent: Tuesday, 20 October 2009 12:11 a.m. To: Rene Subject: Re: [R] loop and plot Hi Rene, the problem is probably due to the fact that R will send all plots to the same graphical output window. Each next plot just replaces the previous one. if it's only a few plots, you can divide the graphical window with the commands par(mfrow=...) (see ?par) or layout(matrix(...)) (see ?layout). Otherwise you have to ask the window to wait before refreshing. par(ask=TRUE) (see ?par as well) Hope this helps Cheers Joris On Mon, Oct 19, 2009 at 12:14 PM, Rene kaixinma...@gmail.com wrote: Dear all, I am stuck at applying loop function for creating separated plots. I have coding like below: dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b) )) kk = function(f) { ls=as.character(f) pie(dataset.table[ls,],main=ls) box() } kk(1) kk(2) kk(3) By using above code, I can create 3 single plot respectively, but when I type kk(1:3), obviously it will not work. I know I have to vectorise the coding, then I can use command kk(1:3). I try to use loop: kk = function(f) { ls=as.character(f) for (i in length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } kk(1:3) the above code only gives me the last pie plot (ie. kk(3) plot) instead of 3 plots respectively. Can someone please guide me how to revise the loop coding, and produce 3 separated plots one after another on the screen by typing kk(1:3)? Thanks a lot. Rene. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop and plot
Or you open a new graphics window / device as part of the loop, see, e.g., ?windows. Alternatively, you may write the content of the graphics device to a file for each iteration, see, e.g., ?savePlot (but you'd want to make sure that you have a different filename in each iteration, otherwise, you'll again have only one figure...). Michael -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of joris meys Sent: Montag, 19. Oktober 2009 13:12 To: Rene Cc: r-help@r-project.org Subject: Re: [R] loop and plot Hi Rene, the problem is probably due to the fact that R will send all plots to the same graphical output window. Each next plot just replaces the previous one. if it's only a few plots, you can divide the graphical window with the commands par(mfrow=...) (see ?par) or layout(matrix(...)) (see ?layout). Otherwise you have to ask the window to wait before refreshing. par(ask=TRUE) (see ?par as well) Hope this helps Cheers Joris On Mon, Oct 19, 2009 at 12:14 PM, Rene kaixinma...@gmail.com wrote: Dear all, I am stuck at applying loop function for creating separated plots. I have coding like below: dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b ,b) )) kk = function(f) { ls=as.character(f) pie(dataset.table[ls,],main=ls) box() } kk(1) kk(2) kk(3) By using above code, I can create 3 single plot respectively, but when I type kk(1:3), obviously it will not work. I know I have to vectorise the coding, then I can use command kk(1:3). I try to use loop: kk = function(f) { ls=as.character(f) for (i in length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } kk(1:3) the above code only gives me the last pie plot (ie. kk(3) plot) instead of 3 plots respectively. Can someone please guide me how to revise the loop coding, and produce 3 separated plots one after another on the screen by typing kk(1:3)? Thanks a lot. Rene. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop and plot
Rene wrote: Dear all, I am stuck at applying loop function for creating separated plots. I have coding like below: dataset.table - table(data.frame(var1=c(1,2,3,1,2,3,1),colour=c(a,b,c,c,a,b,b) )) kk = function(f) { ls=as.character(f) pie(dataset.table[ls,],main=ls) box() } kk(1) kk(2) kk(3) By using above code, I can create 3 single plot respectively, but when I type kk(1:3), obviously it will not work. I know I have to vectorise the coding, then I can use command kk(1:3). I try to use loop: kk = function(f) { ls=as.character(f) for (i in length(f)) { pie(dataset.table[ls[i],],main=ls[i]) box() } } kk(1:3) the above code only gives me the last pie plot (ie. kk(3) plot) instead of 3 plots respectively. Can someone please guide me how to revise the loop coding, and produce 3 separated plots one after another on the screen by typing kk(1:3)? Thanks a lot. Rene. Your code is probably doing what you want, but over-plotting the graphs so quickly, you only see the last one. Inserting readline(Hit ENTER to proceed.) after your box() statement might give you what you want. Joh __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.