# [R] Consistent behaviour of for-loop

Peter Ehlers ehlers at ucalgary.ca
Thu Apr 15 12:29:40 CEST 2010

```On 2010-04-15 4:03, Uwe Dippel wrote:
> Peter Ehlers wrote:
>>> > par(mfrow=c(1,1))
>>> > qqnorm(rnorm(20))
>>> > qqmath(rnorm(20))
>>> > par(mfrow=c(3,4))
>>> > for(i in 1:12)qqnorm(rnorm(20))
>>> Until here everything works as expected, and the last line prints 12
>>> samples of qqnorm. However,
>>> > for(i in 1:12)qqmath(rnorm(20))
>>> is doing nothing at all.
>>
>> You should always tell us what contributed packages you are using.
>> Here, the qqmath function is from pkg:lattice.
>> Now check FAQ 7.22.
> Thanks, Peter!
> (And to the offline-reply as well!)
> The question is not completely answered in FAQ 7.22, though:
>  > par(mfrow=c(3,4))
>  > for(i in 1:12)print(qqmath(rnorm(20)))
> prints 12 after another; not in (3,4)
>
> Why, and how to print 12 samples on a single sheet?

You are mixing 'traditional' graphics (par(...)) and
'lattice' graphics.
That won't work. In lattice, you use the 'layout' argument to
select the number of columns/rows. This is easiest if you set
up a conditioning variable:

cond <- gl(12, 20, labels = letters[1:12])
x <- rnorm(12*20)
qqmath(~x | cond, layout = c(4, 3))

Note that layout = c(columns, rows), not c(rows, columns).

Since you're new to R, let me also recommend very strongly
that you learn to use the str() function (not needed for
this problem, but undoubtedly indispensible in your further

-Peter Ehlers

>
> Uwe
>
>

--
Peter Ehlers
University of Calgary

```