[R] multiple plots in a function()
Weismann_D at medizin.uni-wuerzburg.de
Weismann_D at medizin.uni-wuerzburg.de
Sat May 20 17:33:04 CEST 2006
Sorry for again asking the same question, but I am still not successfull, also after using grid-package, as recommended previously:
I want to write a function() which generates a graphical output and can be used in a loop to produce several results with a layout like in
par(mfrow=c(5,5))
for ( i in 1:10){
plot(1:10)
}
Here is the (experimental) code:
myfunction <- function(){
vp1 <- viewport(x=0.1, y=.7, w=.8, h=.2, just="left", name="vp1")
vp2 <- viewport(x=.1, y=.5, w=.8, h=.2, just="left", name="vp2")
pushViewport(vp1)
grid.rect(gp=gpar(col="grey"))
grid.text("vp1")
grid.xaxis(main=FALSE)
upViewport()
pushViewport(vp2)
grid.rect(gp=gpar(col="grey"))
grid.text("vp2")
grid.xaxis()
}
And the following loop:
par(mfrow=c(5,5))
for (i in 1:10) {
grid.newpage() # when ommitting this line, the following plots will be plotted as childrens of the afore generated parent
myfunction()
}
In conclusion, every myfunction() result overwrites the output of the previous output and is not plotted side by side as intended.
What to change?
Thanks a lot, Dirk
Dr.med. D. Weismann
Schwerpunkt Endokrinologie/Diabetologie
Medizinische Klinik und Poliklinik I
Universität Würzburg
Josef-Schneider-Str. 2
97080 Würzburg
email: weismann_d at klinik.uni-wuerzburg.de
Telefon: 0931/201-1
-----Ursprüngliche Nachricht-----
Von: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Gesendet: Mi 17.05.2006 03:19
An: Weismann, Dirk
Cc: PAlspach at hortresearch.co.nz; jim at bitwrit.com.au
Betreff: Re: [R] multiple plots in a function()
Use grid graphics
http://www.stat.auckland.ac.nz/~paul/grid/grid.html
and the gridbase package to incorporate classic
graphics in that.
On 5/16/06, Weismann_D at medizin.uni-wuerzburg.de
<Weismann_D at medizin.uni-wuerzburg.de> wrote:
> Thanks a lot, but my problem is not to get a temporary change with par()in myfunction and return to 'oldpar'after finishing. What I want is, that the output of myfunction is handled like one graphic (ie one plot) and therefore I can get the output of myfunction 10times side by side in one window (e.g. mfrow=c(5,5)). But the 'par(mfrow=c(1,2))' inside 'myfunction' makes this impossible.
> I used plot(..,type="n")two times to initialize the graphics in 'myfunction' and filled both with a lot of low-level graphic code. Since I always need both graphical outputs to interpret the results, I prefer to write one function instead of two for each plot. This might not be the best way to create a graphical output in a function, but how to do it better?
>
> Thanks, Dirk
>
> -----Ursprüngliche Nachricht-----
> Von: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von Gabor Grothendieck
> Gesendet: Dienstag, 16. Mai 2006 05:01
> An: Weismann, Dirk
> Cc: r-help at stat.math.ethz.ch
> Betreff: Re: [R] multiple plots in a function()
>
> You could override par by optionally passing it as an argument:
>
> f <- function(x = 1:10, y = 1:10, par = list(mfrow = c(2,2))) {
> if (!is.null(par)) {
> on.exit(par(opar))
> opar <- par(par)
> }
> plot(x)
> plot(y)
> }
>
> opar <- par(mfrow=c(4,4))
> for(i in 1:8) f(par = NULL)
> par(opar)
>
>
>
> On 5/15/06, Weismann_D at medizin.uni-wuerzburg.de
> <Weismann_D at medizin.uni-wuerzburg.de> wrote:
> > Dear all,
> > I have the following problem:
> > I have written a function genereating to plots, eg myfunction <-
> > (data, some.parameters) {
> > #some calculations etc
> > .
> > par (mfrow=c(1,2))
> > plot1(......)
> > plot2(.....)
> > }
> > which works fine. But for analysing several variants, I tried a slope, eg:
> >
> > par (mfrow=c(5,5))
> > for ( i in 1:10) {
> > myfunction(data, i)
> > }
> >
> > Off course, the par() in myfunction overwrites the par() before the slope. So, how to write myfunction, that it plots two plots and can be used in the slope like in the example?
> >
> > Thanks a lot, Dirk
> >
> > Dr.med Dirk Weismann
> > Schwerpunkt für Endokrinologie und Diabetologie Medizinische
> > Universitätsklinik I 97080 Würzburg
> > email: weismann_d at klinik.uni-wuerzburg.de
> > Telefon: 0049-931-201-36744
> >
> > [[alternative HTML version deleted]]
> >
> >
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list