簡體   English   中英

在R圖中添加x軸下方的箭頭

[英]Adding an arrow below the x axis in R plots

我試圖在R圖中添加標記x軸下方特定x坐標的箭頭。 我的x軸在y = 0時,當我嘗試在arrows使用負y坐標時,箭頭將垂直於x軸,我只得到箭頭的邊緣(盡管是一些空間,e,g其中繪制了x軸標簽和刻度線)。

可以在箭頭中使用xpd選項,這樣您就可以將坐標設置在繪圖區域之外,並將xpd設置為TRUE。 例如,假設xlim = c(0,10)和ylim =(0,10),並將x軸設置為0然后

arrows(1.4, -1, 1.4, 0, xpd = TRUE)

繪制一個垂直箭頭,指向該軸上位置1.4處的x軸。

您可以通過添加額外的疊加來執行此操作,方法是調用par(new=TRUE) ,邊距減少。 例如:

plot(1,1) ## start a plot
opar <- par(new = TRUE, ## add a new layer
            mar = c(0,0,0,0)) ## with no margins margins
## set up the plotting area for this layer
plot(1,1,xlim=c(0,1),ylim=c(0,1),type='n',xlab='',ylab='') 
arrows(0.1,0.05,0.5,0.05) ## add arrow
par(opar) ## return the plot parameters to their prior values

編輯:如果要保持原始圖中的坐標,則必須仔細選擇x軸和y軸限制。 這說明了:

plot(1,1,xlim=0:1,ylim=0:1)
arrows(0.1,0.05,0.5,0.05)
gpar <- par()
opar <- par(new = TRUE, mar = c(0,0,0,0),xaxs='i',yaxs='i')

m1 <- (gpar$usr[2] - gpar$usr[1])/(gpar$plt[2] - gpar$plt[1])
c1 <- gpar$usr[1] - m1*gpar$plt[1]
m2 <- (gpar$usr[4] - gpar$usr[3])/(gpar$plt[4] - gpar$plt[3])
c2 <- gpar$usr[3] - m2*gpar$plt[3]
xlim <- c(c1, m1 + c1)
ylim <- c(c2, m2 + c2)

plot(1,1,xlim=xlim,ylim=ylim,type='n',xlab='',ylab='')
arrows(0.1,0.05,0.5,0.05,col='red')
points(1,1,col='red')
par(opar)

暫無
暫無

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

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