Skip to content
Prev 394420 / 398500 Next

plot level, velocity, acceleration with one x axis

On 5/31/23 9:20 AM, Eric Berger wrote:
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)


 > sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.7.7

Matrix products: default
BLAS: 
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 

LAPACK: 
/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; 
  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_4.3.0  tools_4.3.0     rstudioapi_0.14