我有一个图,可以得到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)
但是,我试图显示RHS y轴上的最后一个值的百分比变化,但我不确定如何执行此操作。谢谢您的帮助。
答案 0 :(得分:1)
这可以通过以下步骤实现:
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)
编辑: 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)