Spencer Graves
2023-May-29  04:57 UTC
[R] plot level, velocity, acceleration with one x axis
Hello, All:
	  I want to plot level, velocity, and acceleration in three panels with 
only one x axis.  The code below does this using "layout".  However, I
want the three plot areas to be of equal size, and this won't do that: 
If I stretch the plot vertically, the relative sizes of the three panels 
changes.  There's probably a way to do this with ggplot2, but I have yet 
to find it.
	  Suggestions?
	  Thanks,
	  Spencer Graves
str(AirTime <- as.numeric(time(AirPassengers)))
str(AP <- as.numeric(AirPassengers))
def.par <- par(no.readonly = TRUE) # save default, for resetting...
(mat3x1 <- matrix(1:3, 3))
plot3x1 <- layout(mat3x1, heights=c(1.4, 1, 1.5))
layout.show(plot3x1)
par(mar=c(0, 4.1, 4.1, 2.1))
plot(AirTime, AP, log='y', type='l', axes=FALSE,
      main='AirPassengers', ylab='AirPassengers')
box(col='grey')
axis(2, las=1)
par(mar=c(0, 4.1, 0, 2.1))
vAP <- diff(log(AP))
plot(tail(AirTime, -1), vAP, type='l',
      ylab='percent increase', axes=FALSE)
box(col='grey')
axis(2, las=1)
par(mar=c(5.1, 4.1, 0, 2.1))
plot(tail(AirTime, -2), diff(vAP), type='l',
      ylab='acceleration', xlab='year',
      las=1)
box(col='grey')
par(def.par)
How about this:
a <- cbind(AirPassengers, diff(log(AirPassengers)),
diff(diff(log(AirPassengers))))
colnames(a)[2:3] <- c("percent increase", "acceleration")
plot(a, xlab="year", main="AirPassengers")
HTH,
Eric
On Mon, May 29, 2023 at 7:57?AM Spencer Graves
<spencer.graves at effectivedefense.org> wrote:>
> Hello, All:
>
>
>           I want to plot level, velocity, and acceleration in three panels
with
> only one x axis.  The code below does this using "layout". 
However, I
> want the three plot areas to be of equal size, and this won't do that:
> If I stretch the plot vertically, the relative sizes of the three panels
> changes.  There's probably a way to do this with ggplot2, but I have
yet
> to find it.
>
>
>           Suggestions?
>           Thanks,
>           Spencer Graves
>
>
> str(AirTime <- as.numeric(time(AirPassengers)))
> str(AP <- as.numeric(AirPassengers))
>
> def.par <- par(no.readonly = TRUE) # save default, for resetting...
> (mat3x1 <- matrix(1:3, 3))
> plot3x1 <- layout(mat3x1, heights=c(1.4, 1, 1.5))
> layout.show(plot3x1)
>
> par(mar=c(0, 4.1, 4.1, 2.1))
> plot(AirTime, AP, log='y', type='l', axes=FALSE,
>       main='AirPassengers', ylab='AirPassengers')
> box(col='grey')
> axis(2, las=1)
>
> par(mar=c(0, 4.1, 0, 2.1))
> vAP <- diff(log(AP))
> plot(tail(AirTime, -1), vAP, type='l',
>       ylab='percent increase', axes=FALSE)
> box(col='grey')
> axis(2, las=1)
>
> par(mar=c(5.1, 4.1, 0, 2.1))
> plot(tail(AirTime, -2), diff(vAP), type='l',
>       ylab='acceleration', xlab='year',
>       las=1)
> box(col='grey')
>
> par(def.par)
>
> ______________________________________________
> R-help at 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.