简体   繁体   English

R%从RHS y轴上的最后一个值改变为与LHS y轴刻度值相对应的位置

[英]R %change from last value on RHS y axis to correspond in position with LHS y axis scale values

I have a plot and can get a LHS y axis and RHS yaxis 我有一个图,可以得到一个LHS y轴和RHS y轴

x <- rnorm(100)
y <- cumsum(x) 

lastval <- tail(y,1)
pchange <- (y-lastval)*100/lastval
plot(1:100,y)

axis(side = 4) 

However I am trying to show the %change from the last value on the RHS y axis but I am uncertain how to do this. 但是,我试图显示相对于RHS y轴上的最后一个值的百分比变化,但是我不确定如何执行此操作。 Thank you for your help. 谢谢您的帮助。

This can be achieved in the following set of steps: 这可以通过以下步骤实现:

par(mar = c(5, 5, 3, 5)) # right side margin must be adjusted for mtext
plot(1:100, y)
par(new=TRUE)
plot(1:100, pchange, axes = FALSE, ylab="", xlab="") #axes, ylab and xlab prevents overwritting
axis(side = 4) # adds the right scale
mtext("Change in [%]", side=4, line=3) 

EDIT: Values of RHS and LHS are linked: 编辑: RHS和LHS的值链接:

set.seed(123)
x <- rnorm(100)
y <- cumsum(x) 

lastval <- tail(y,1)
labels <- round(100*(seq(-2,10,2)-lastval)/lastval, 1)

plot(1:100,y, main=(paste("Last Value is ", round(lastval,2))) )
axis(side = 4, at=seq(-2,10,2), labels=labels)
abline(h=seq(-2,10,2), col="grey")
mtext("Change in [%]", side=4, line=3)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM