[R] loop and plot

joris meys jorismeys at gmail.com
Mon Oct 19 13:11:59 CEST 2009


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 <kaixinmalea at 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 at 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.
>




More information about the R-help mailing list