[R] plot level, velocity, acceleration with one x axis
Deepayan Sarkar
deep@y@n@@@rk@r @end|ng |rom gm@||@com
Thu Jun 1 06:04:48 CEST 2023
I think your proposal of modifying plot.ts() to allow 'log' to be
vectorized would be the most natural solution here.
For what it's worth, the details of the implementation and the fact
that you can supply a panel function allows an ugly hack:
pfun <- function(...) {
e <- parent.frame()
e$log <- ""
lines(...)
}
plot(DAX., log = "y", panel = pfun)
This would need to be modified to include a counter in more
complicated cases; we can do without because only the first panel has
a log scale in this example.
-Deepayan
On Thu, Jun 1, 2023 at 12:53 AM Spencer Graves
<spencer.graves using effectivedefense.org> wrote:
>
>
>
> On 5/31/23 2:12 PM, Viechtbauer, Wolfgang (NP) wrote:
> > How about using the same 'mar' for all plots, but adding an outer margin?
> >
> > DAX <- EuStockMarkets[, 'DAX']
> > DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX))))
> > colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> > head(DAX.)
> >
> > par(mfrow=c(3,1), mar=c(1,4.5,0,2), oma=c(3,0,1,0))
> >
> > plot(DAX.[, 1], log='y', ylab='DAX', axes=FALSE)
> > axis(2)
> > box(col='grey')
> >
> > plot(DAX.[, 2], ylab='vel (%)', axes=FALSE)
> > axis(2)
> > box(col='grey')
> >
> > plot(DAX.[, 3], ylab='accel (%)', axes=FALSE)
> > axis(2)
> > box(col='grey')
> > axis(1)
> >
> > Best,
> > Wolfgang
>
>
> That's exactly what I needed.
>
>
> Thanks, Spencer
> >
>
> >> -----Original Message-----
> >> From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Spencer Graves
> >> Sent: Wednesday, 31 May, 2023 17:45
> >> To: Eric Berger
> >> Cc: r-help
> >> Subject: Re: [R] plot level, velocity, acceleration with one x axis
> >>
> >> On 5/31/23 9:20 AM, Eric Berger wrote:
> >>> I sent you an updated response to deal with the redundant copies of the x-axis.
> >>> Re-sending.
> >>>
> >> par(mfrow=c(3,1))
> >> plot(DAX.[, 1], log='y', ylab='DAX', xaxt="n")
> >> plot(DAX.[, 2], ylab='vel (%)', xaxt="n")
> >> plot(DAX.[, 3], ylab='accel (%)')
> >>
> >> I got that. The primary problem with that is that most of the
> >> vertical space is reserved for axis labels, whether they are printed or
> >> not. If I squeeze the vertical dimension of the plot, I get, "figure
> >> margins too large". To control that, I need to set "mar" separately for
> >> each panel, and then the plot regions for each are not the same size.
> >> Using the "layout" function instead of "mfrow" is better, but I don't
> >> see now to make that work consistently without fixing the aspect ratio.
> >> There may be a way in the tidyverse, but I haven't found it yet. The
> >> only solution I've found so far that makes sense to me is to modify the
> >> code for plot.ts to accept a vector for the log argument, with the
> >> constraint that length(lot) = either 1 or ncol(x) and returning
> >> invisibly an object that would make it feasible for a user to call
> >> axis(2, ...) once for each vertical axis to handle cases where someone
> >> wanted to a vertical scale different from linear and log. I'd want to
> >> make sure that lines.ts also works with this, because I want to add fits
> >> and predictions.
> >>
> >> Comments?
> >> Thanks,
> >> Spencer Graves
> >>
> >> ** With either of the following plots, if I adjust the aspect ratio by
> >> enlarging or reducing the vertical dimension of the plot, the relative
> >> sizes of the plot regions change.
> >>
> >> DAX <- EuStockMarkets[, 'DAX']
> >> DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX))))
> >> colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> >> head(DAX.)
> >>
> >> plot(DAX., log='xy')
> >>
> >> op <- par(mfrow=c(3,1), mar=c(0, 4.1, 4.1, 2.1))
> >> plot(DAX.[, 1], log='y', ylab='DAX', axes=FALSE)
> >> axis(2)
> >> box(col='grey')
> >> par(mar=c(0, 4.1, 0, 2.1))
> >> plot(DAX.[, 2], ylab='vel (%)', axes=FALSE)
> >> axis(2)
> >> box(col='grey')
> >> par(mar=c(5.1, 4.1, 0, 2.1))
> >> plot(DAX.[, 3], ylab='accel (%)', axes=FALSE)
> >> axis(2)
> >> box(col='grey')
> >> axis(1)
> >> par(op)
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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