簡體   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

我有一個圖,可以得到一個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軸上的最后一個值的百分比變化,但是我不確定如何執行此操作。 謝謝您的幫助。

這可以通過以下步驟實現:

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM