[R] plot level, velocity, acceleration with one x axis

Spencer Graves @pencer@gr@ve@ @end|ng |rom e||ect|vede|en@e@org
Tue May 30 12:46:03 CEST 2023



On 5/29/23 2:37 AM, Eric Berger wrote:
> 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")


	  My real problem is more difficult:  I'm analyzing CO2 data from Our 
World in Data (https://ourworldindata.org/co2-emissions), and I need to 
plot the CO2 data on a log scale but velocity and acceleration on linear 
scales.  The following is comparable:


str(DAX <- EuStockMarkets[, 'DAX'])
str(DAX. <- cbind(DAX, diff(log(DAX)),
                   diff(diff(log(DAX)))))
colnames(DAX.)[2:3] <- c('vel', 'accel')
plot(DAX.)


	  I want the first of the three panels to plot on the log scale, but 
the other two on linear scales.  The obvious attempt does not work:


plot(DAX., log=c('y', '', ''))
#Error in length(log) && log != "" :
#  'length = 3' in coercion to 'logical(1)'


	  Trying to construct my own axes isn't easy, either:


str(logDAX <- cbind(log(DAX), diff(log(DAX)),
                   diff(diff(log(DAX)))))
colnames(logDAX) <- c('logDAX', 'vel', 'accel')
plot(logDAX, axes=FALSE)
axis(1)
axis(2)


	  I'm thinking of creating my own copy of "plot.ts", and changing it so 
it accepts the "log" argument as a vector of length equal to ncol of the 
ts object to be plotted AND returning an object that would allow a user 
to call "axis" ncol times.


	  Suggestions?


	  Thanks,
	  Spencer Graves

> 
> HTH,
> Eric
> 
> 
> On Mon, May 29, 2023 at 7:57 AM Spencer Graves
> <spencer.graves using 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 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