Run function several times changing only one argument - without a loop

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Run function several times changing only one argument - without a loop

Marion Wenty
Dear people,

I created a plot which looks like this:

Ee1<-matrix(c(88,86,74,62,41),ncol=5)
colnames(Ee1)<-c("Lehrer","Lehrerinnen","Klassenkollegen","Klassenkolleginnen","Geschwister")
par(las=1)
par(mar=c(5,13,4,2))
barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
axis(2,pos=10, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=20, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=30, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=40, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=50, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=60, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=70, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=80, tick=T, tcl=F, labels=F,col="white")
axis(2,pos=90, tick=T, tcl=F, labels=F,col="white")

Now I would like to shorten the whole thing - namely use only one step to
create the 9 axes without having to use a loop.

In general, I would be interested if there is a way to use a function
several times changing only one argument, without having to use a loop.

Does anyone know how to do that.

Marion

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Run function several times changing only one argument - without a loop

Enrico Schumann

Hi Marion,

is all you want the white vertical lines? Then try

abline(v = seq(10, 90, by = 10), col = "white")

instead of your axis commands.


Regards,
Enrico

Am 20.02.2012 11:04, schrieb Marion Wenty:

> Dear people,
>
> I created a plot which looks like this:
>
> Ee1<-matrix(c(88,86,74,62,41),ncol=5)
> colnames(Ee1)<-c("Lehrer","Lehrerinnen","Klassenkollegen","Klassenkolleginnen","Geschwister")
> par(las=1)
> par(mar=c(5,13,4,2))
> barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
> axis(2,pos=10, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=20, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=30, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=40, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=50, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=60, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=70, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=80, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=90, tick=T, tcl=F, labels=F,col="white")
>
> Now I would like to shorten the whole thing - namely use only one step to
> create the 9 axes without having to use a loop.
>
> In general, I would be interested if there is a way to use a function
> several times changing only one argument, without having to use a loop.
>
> Does anyone know how to do that.
>
> Marion
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

--
Enrico Schumann
Lucerne, Switzerland
http://nmof.net/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Run function several times changing only one argument - without a loop

Eik Vettorazzi-2
In reply to this post by Marion Wenty
Hi Marion,
you can either use any of the *apply-functions or vectorize your
function (which internally uses mapply):

par(las=1)
par(mar=c(5,13,4,2))
barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)

#using sapply
invisible(sapply((1:9)*10,function(x)axis(2,pos=x,tick=T, tcl=F,
labels=F,col="white")))

#using Vectorize
barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
vaxis<-Vectorize(axis,"pos")
invisible(vaxis(2,pos=(1:9)*10, tick=T, tcl=F, labels=F,col="white"))

Cheers!

Am 20.02.2012 11:04, schrieb Marion Wenty:

> Dear people,
>
> I created a plot which looks like this:
>
> Ee1<-matrix(c(88,86,74,62,41),ncol=5)
> colnames(Ee1)<-c("Lehrer","Lehrerinnen","Klassenkollegen","Klassenkolleginnen","Geschwister")
> par(las=1)
> par(mar=c(5,13,4,2))
> barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
> axis(2,pos=10, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=20, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=30, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=40, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=50, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=60, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=70, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=80, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=90, tick=T, tcl=F, labels=F,col="white")
>
> Now I would like to shorten the whole thing - namely use only one step to
> create the 9 axes without having to use a loop.
>
> In general, I would be interested if there is a way to use a function
> several times changing only one argument, without having to use a loop.
>
> Does anyone know how to do that.
>
> Marion
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.


--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Run function several times changing only one argument - without a loop

Marion Wenty
Hi Enrico,
Yes, you were right, I just wanted to draw white vertical lines and with
your command it's much simpler. Thanks a lot!

Hi Eik,
I learned a lot from your tips about sapply and Vectorize!
I had only used apply, lapply and mapply but not yet sapply.
I find the Vectorize function very fascinating: being able to change a
function - this is something I hadn't come accross yet.
By checking out the command "invisible" I also learned a new command.
Thank you very much!

Marion


Hi Marion,

is all you want the white vertical lines? Then try

abline(v = seq(10, 90, by = 10), col = "white")

instead of your axis commands.


Regards,
Enrico

2012/2/20 Eik Vettorazzi <[hidden email]>

> Hi Marion,
> you can either use any of the *apply-functions or vectorize your
> function (which internally uses mapply):
>
> par(las=1)
> par(mar=c(5,13,4,2))
> barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
>
> #using sapply
> invisible(sapply((1:9)*10,function(x)axis(2,pos=x,tick=T, tcl=F,
> labels=F,col="white")))
>
> #using Vectorize
> barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
> vaxis<-Vectorize(axis,"pos")
> invisible(vaxis(2,pos=(1:9)*10, tick=T, tcl=F, labels=F,col="white"))
>
> Cheers!
>
> Am 20.02.2012 11:04, schrieb Marion Wenty:
> > Dear people,
> >
> > I created a plot which looks like this:
> >
> > Ee1<-matrix(c(88,86,74,62,41),ncol=5)
> >
> colnames(Ee1)<-c("Lehrer","Lehrerinnen","Klassenkollegen","Klassenkolleginnen","Geschwister")
> > par(las=1)
> > par(mar=c(5,13,4,2))
> > barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
> > axis(2,pos=10, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=20, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=30, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=40, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=50, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=60, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=70, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=80, tick=T, tcl=F, labels=F,col="white")
> > axis(2,pos=90, tick=T, tcl=F, labels=F,col="white")
> >
> > Now I would like to shorten the whole thing - namely use only one step to
> > create the 9 axes without having to use a loop.
> >
> > In general, I would be interested if there is a way to use a function
> > several times changing only one argument, without having to use a loop.
> >
> > Does anyone know how to do that.
> >
> > Marion
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
>
> --
> Eik Vettorazzi
> Institut für Medizinische Biometrie und Epidemiologie
> Universitätsklinikum Hamburg-Eppendorf
>
> Martinistr. 52
> 20246 Hamburg
>
> T ++49/40/7410-58243
> F ++49/40/7410-57790
>
> --
> Pflichtangaben gemäß Gesetz über elektronische Handelsregister und
> Genossenschaftsregister sowie das Unternehmensregister (EHUG):
>
> Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen
> Rechts; Gerichtsstand: Hamburg
>
> Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden),
> Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus
>
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Run function several times changing only one argument - without a loop

David Winsemius
In reply to this post by Marion Wenty

On Feb 20, 2012, at 5:04 AM, Marion Wenty wrote:

> Dear people,
>
> I created a plot which looks like this:
>
> Ee1<-matrix(c(88,86,74,62,41),ncol=5)
> colnames(Ee1)<-
> c
> ("Lehrer
> ","Lehrerinnen","Klassenkollegen","Klassenkolleginnen","Geschwister")
> par(las=1)
> par(mar=c(5,13,4,2))
> barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
> axis(2,pos=10, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=20, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=30, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=40, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=50, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=60, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=70, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=80, tick=T, tcl=F, labels=F,col="white")
> axis(2,pos=90, tick=T, tcl=F, labels=F,col="white")
>
> Now I would like to shorten the whole thing - namely use only one  
> step to
> create the 9 axes without having to use a loop.

See if grid() is helpful:

barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
grid(nx=9, ny=NA, col="white")

Somehow using axis to draw elements inside a graph seems "wrong",  
especially when you are using it for what is basically  side-effect as  
you have been attempting. Could also have used abline which accepts a  
vector for its v argument.

 > barplot(Ee1,horiz=T,col="grey85",border="NA",xlim=c(0,100),axes=F)
 > abline(v=seq(10,90,by=10), col="white")


>
> In general, I would be interested if there is a way to use a function
> several times changing only one argument, without having to use a  
> loop.

A large number of functions will offer that facility using seq() as  
the functional input argument.
>
> Does anyone know how to do that.
>
> Marion
>
> [[alternative HTML version deleted]]

You might consider working in the settings for your mail client. I  
know that gmail offers that option.

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.