gpt4 book ai didi

r - 添加多图轴

转载 作者:行者123 更新时间:2023-12-04 05:44:27 30 4
gpt4 key购买 nike

为了生成具有多个图的布局,我有以下带有一些虚拟图的代码:

jpeg("/path/to/file",height=10000,width=5000)
plot.new()
par(mar=c(2,2,1,1), oma=c(2,4,0,0), xpd=NA)

for (i in 1:10) {

par(mar=c(2,2,1,1),fig=c(0, 0.5, (10-i)/10, (11-i)/10), new=T)
matplot(rnorm(20)*sample(100,1),
col="blue",axes=F,type="l",lwd=10, xlab="",ylab="")

par(mar=c(2,2,1,1),fig=c(0.5, 1, (10-i)/10, (11-i)/10), new=T)
matplot(rnorm(20)*sample(100,1),
col="red",axes=F,type="l",lwd=10, xlab="",ylab="")
}
dev.off()

我想在跨越一列中的所有 10 个图的远 LHS 和远 RHS 上添加一条垂直线/轴。由于我将使用这条线作为轴,我需要能够添加刻度和标签。

最佳答案

您可以通过 ?axis 绘制轴或 ?Axis .要在多个图上跨越您的轴,您必须重置 usr 坐标。

请在下面找到基本图形解决方案:

## store number of rows
nRow <- 10

## your example code
## (only the number "10" is replaced by nRow and oma is adapted)
plot.new()

par(mar=c(2, 2, 1, 1), oma=c(2, 4, 0, 4), xpd=NA)

for (i in 1:nRow) {

par(mar=c(2, 2, 1, 1), fig=c(0, 0.5, (nRow-i)/nRow, ((nRow+1)-i)/nRow), new=TRUE)
matplot(rnorm(20)*sample(100, 1),
col="blue", axes=F, type="l", lwd=10, xlab="", ylab="")

par(mar=c(2, 2, 1, 1), fig=c(0.5, 1, (nRow-i)/nRow, ((nRow+1)-i)/nRow), new=TRUE)
matplot(rnorm(20)*sample(100, 1),
col="red", axes=F, type="l", lwd=10, xlab="", ylab="")
}

## define new user coordinates
usr <- c(0, 1, 0, 1) ## x1, x2, y1, y2

## calculate tick positons
## in general: (usr[3]+(diff(usr[3:4])/(nRow-1))*0:(nRow-1))
## but our usecase is much easier:
ticksAt <- 1/(nRow-1)*0:(nRow-1)

## choose left column and reset user plotting area (usr)
par(mar=c(2, 2, 1, 1), fig=c(0, 0.5, 0, 1), usr=usr, new=TRUE)
## draw axis; see ?Axis for details
Axis(side=2, at=ticksAt, labels=as.character(1:(nRow)), line=0.5)

## choose right column and reset user plotting area (usr, not needed because already done)
par(mar=c(2, 2, 1, 1), fig=c(0.5, 1, 0, 1), usr=usr, new=TRUE)
## draw axis; see ?Axis for details
Axis(side=4, at=ticksAt, labels=as.character((nRow+1):(2*nRow)), line=0.5)

关于r - 添加多图轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867283/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com