statquant2 wrote on 09/28/2011 10:01:30 AM:>
> Hello I am trying to write a function that would plot timeseries
easily...> I aim at plotting two time-series on 2 different y axis sharing the same
> x-axis
>
> I succeded in doing so:
> plotTimeSerie(x,y,y2,[a lot of other args]){
> ...
> plot()
> axis.POSIXct(side=1) #here I build the x-axis
> points() #here I plot the first time serie related to y-axis
> ...
> axis(side=2,[some args])
> text(side=2,text="",[some args]) #here I build the y-left axis
and
annotate> with text
> ...
> points() #here I plot the first time serie related to y-right axis
> axis(side=4,[some args])
> text(side=2,text="",[some args]) #here I build the y-right axis
and
annotate> with text
>
> }
>
> My problem is that I would like the user to be able to specify any
> parameters he wishes for the axis/points functions.
> How can I make plotTimeSerie handling any parameters and pass them into
the> specified functions (points,axis...)?
You'll have to create argument names that specify which function they are
to be passed to. For example:
myplot <- function(x, y, pt.col="black", ax1.col="black",
ax2.col="black")
{
plot(x, y, type="n", axes=F)
points(x, y, col=pt.col)
axis(1, col=ax1.col)
axis(2, col=ax2.col)
}
myplot(1:10, 1:10, pt.col="red", ax1.col="blue",
ax2.col="orange")
If there was one function that you thought would need the most
specifications, you could use ellipses (...) to allow for inclusion of any
arguments. For example:
myplot <- function(x, y, ax1.col="black",
ax2.col="black", ...) {
plot(x, y, type="n", axes=F)
points(x, y, ...)
axis(1, col=ax1.col)
axis(2, col=ax2.col)
}
myplot(1:10, 1:10, ax1.col="blue", ax2.col="orange", pch=16,
col="red",
cex=3)
Jean
[[alternative HTML version deleted]]