2011-06-22 13 views
2

我想就用,R 4軸的陰謀,以便它類似於這樣的情節:如何使用R製作以下圖形?

enter image description here

我已經看過了Quick-R website徵求意見和修改自己的例子之一(稱爲A Silly Axis Example ):

# specify the data 
x <- c(1:5); y <- x/2; 
w <- c(2:4) 
z <- c(1:5) 

# create extra margin room on the right for an axis 
par(mar=c(5, 4, 4, 8) + 0.1) 

# plot x vs. y 
plot(x, y,type="b", pch=21, col="red", 
    yaxt="n", lty=3, xlab="", ylab="") 

# add x vs. 1/x 
lines(x, z, type="b", pch=22, col="blue", lty=2) 

# draw an axis on the left 
axis(2, at=x,labels=x, col.axis="red", las=2) 

# draw an axis on the right, with smaller text and ticks 
axis(4, at=w,labels=round(w,digits=2), 
    col.axis="blue", las=2, cex.axis=0.7, tck=-.01) 

# draw an axis on the top 
axis(3, at=z,labels=round(z,digits=2), 
    col.axis="blue", las=2, cex.axis=0.7, tck=-.01) 

# add a title for the right axis 
mtext("L", side=3, line=3, cex.lab=1,las=2, col="blue") 

# add a title for the right axis 
mtext("OSR", side=4, line=3, cex.lab=1,las=2, col="red") 

# add a main title and bottom and left axis labels 
title("", xlab="GSI", ylab="FSI") 

此代碼產生以下情節: enter image description here

我有DIF很難弄清楚不同的軸可以具有不同的尺度。例如,我希望頂軸L從5到13,但如果我設置了z <-c(5:13),它不會將軸設置爲這些值。不過,我可以覆蓋哪些標籤:

axis(3, at=z,labels=round(c(9:13),digits=2), col.axis="blue", 
las=2, cex.axis=0.7, tck=-.01) 

但當時如果我想用繪製這四個參數的一個點,點不會出現在正確的位置顯示出來。我應該怎麼做?

回答

3

一個(也許很麻煩)選項是編寫轉換函數,它們可以在兩個尺度之間轉換值。假設你知道的頂部和底部的兩個軸的時間提前的數據範圍,你可以寫這樣的功能:

convertScaleToBottom <- function(x,botRange,topRange){ 
    temp <- (x - topRange[1])/(topRange[2] - topRange[1]) 
    return(botRange[1] + (temp * (botRange[2] - botRange[1]))) 
} 

,將一組值,x,在頂軸刻度並將其轉換爲底部軸標度。然後可以繪製轉換後的值,並保留原稿作爲標籤:

z1 <- 5:13 
z1Adj <- convertScaleToBottom(z1,range(x),range(z1)) 

# draw an axis on the top 
axis(3, at=z1Adj,labels=round(z1,digits=2), 
    col.axis="blue", las=2, cex.axis=0.7, tck=-.01) 

此方法容易地修改以反轉頂軸的順序:

convertScaleToBottomRev <- function(x,botRange,topRange){ 
    temp <- (x - topRange[1])/(topRange[2] - topRange[1]) 
    return(botRange[2] - (temp * (botRange[2] - botRange[1]))) 
} 
+0

感謝您的回答Joran;它運作良好。只需要澄清一下,我可以使用次軸繪製一個點,並且會認爲這些軸的刻度線間距? – djq

相關問題