gpt4 book ai didi

r - R 图中行的恒定绝对间距

转载 作者:行者123 更新时间:2023-12-02 01:45:49 25 4
gpt4 key购买 nike

R 中,如何调整绘图设备的 height 值(例如,windows() png()),以便点之间的绝对间距保持不变?这是一个例子来说明我的意思:

n <- 10
windows(width=4, height=4)
par(xpd=NA)
plot(1:n)
abline(h=1:10)

n <- 20
windows(width=4, height=6)
par(xpd=NA)
plot(1:n)
abline(h=1:20)

我希望每个图中的点/线之间的距离完全相同(我使用了 par(xpd=NA) 因此很容易将图表彼此相邻放置并且如果线条对齐,可以看得更清楚)。显然,height 需要是 n 的函数。人们当然可以通过反复试验来尝试找到 n 的某些功能来实现这一点。但也许有一种聪明的方法可以通过编程实现这一目标。

我的问题与此类似:set ggplot plots to have same x-axis width and same space between dot plot rows .但是这个问题使用 ggplot2,而我的问题坚持基本图形。

最佳答案

这里似乎有两件事在起作用。

  1. R 如何处理轴的分割?
  2. 利润率对这些规则有什么影响?

首先我们需要弄清楚没有边距会发生什么:

adjLineSpace <- function(nLine, baseHeight=32){
# get reproducible device
png(paste("nLine=", nLine, ".png", sep=""), width=baseHeight*nLine, heigh=baseHeight*nLine, res=100)
# control margins
par(mar=c(0,0,0,0))
# open plot with axis that do not interact with data to find "optimal range of axes"
plot(0:nLine, pch=NA_integer_, xlim=c(0, nLine), ylim=c(0, nLine), yaxs="i", xaxs="i")
abline(h=1:nLine)
# include text for visualisation
text(0:nLine, 0:nLine, labels=0:nLine)
text(c(1:4), 0.5, par("usr")[1:4], pos=4)
text(c(5), c(0.5), paste("pin: ", par("pin")[1], ", ", par("pin")[2], sep=""), pos=4)
text(c(5), c(1.5), paste("fin: ", par("fin")[1], ", ", par("fin")[2], sep=""), pos=4)
text(c(5), c(2.5), paste("din: ", par("din")[1], ", ", par("din")[2], sep=""), pos=4)
dev.off()
}
# examples
adjLineSpace(10)
adjLineSpace(20)

找到问题后感觉像个菜鸟,好久没看到了:需要从0开始数,否则不会有10、20或n“行”,而是9 , 19 或 n-1。


但由于几乎任何场景都需要边距,我们还需要找出边距如何与其相互作用:

# introduce new attributes to adjust margins, make it possible to deal with width and height seperately and be able to controll resolution
adjLineSpace <- function(nLine, basePHeight=32, basePWidth=32, marHeight=c(0, 0), marWidth=c(0, 0), res=100){
pMai <- round(par("mai"),2); pMar <- round(par("mar"),2)
pixelLeft <- marWidth[1]*pMai[2]/pMar[2]*res # adjust left
pixelRight <- marWidth[2]*pMai[4]/pMar[4]*res # adjust right
pixelBottom <- marHeight[1]*pMai[1]/pMar[1]*res # adjust bottom
pixelTop <- marHeight[2]*pMai[3]/pMar[3]*res # adjust top
# reproducible device: we first need to find out how many inches one row covers, multiply that by our resolution and the number of 'lines' we want to use in our margin
png(
paste("nLine=", nLine, ".png", sep=""),
width=basePWidth*nLine + pixelLeft + pixelRight,
heigh=basePHeight*nLine + pixelBottom + pixelTop,
res=res
)
# set margins
par(mar=c(marHeight[1], marWidth[1], marHeight[2], marWidth[2]))
# this is the key to it all! Set range of plot area (in pixels) as above, only transformed to inches (divided by resolution in ppi=points/pixels per inch)
par(pin=c(basePWidth*nLine/res, basePHeight*nLine/res))
plot(0:nLine, pch=NA_integer_, xlim=c(0, nLine), ylim=c(0, nLine), yaxs="i", xaxs="i", bg="white")
abline(h=1:nLine)
# include text for visualisation of values
text(0:nLine, 0:nLine, labels=0:nLine)
text(c(1:4), 0.5, par("usr")[1:4], pos=4)
text(c(5), c(0.5), paste("din: ", par("din")[1], ", ", par("din")[2], sep=""), pos=4)
text(c(5), c(1.5), paste("fin: ", par("fin")[1], ", ", par("fin")[2], sep=""), pos=4)
text(c(5), c(2.5), paste("pin: ", par("pin")[1], ", ", par("pin")[2], sep=""), pos=4)
text(c(.5), c(9.5), paste("mar: ", pMar[1], ", ", pMar[2], ", ", pMar[3], ", ", pMar[4], sep=""), pos=4)
text(c(.5), c(8.5), paste("mai: ", pMai[1], ", ", pMai[2], ", ", pMai[3], ", ", pMai[4], sep=""), pos=4)
text(c(.5), c(7.5), paste("mai(nch)/mar(ow): ", round(pMai[1]/pMar[1],2), ", ", round(pMai[2]/pMar[2],2), ", ", round(pMai[3]/pMar[3],2), ", ", round(pMai[4]/pMar[4],2), sep=""), pos=4)
dev.off()
}
# examples without margins still work
adjLineSpace(10, basePHeight=32, basePWidth=32, marHeight=c(0, 0), marWidth=c(0, 0))
adjLineSpace(20, 32, 32, c(0, 0), c(0, 0))
# with margins as well!
adjLineSpace(10, 32, 32, c(4, 0), c(4, 0))
adjLineSpace(20, 32, 32, c(4, 0), c(4, 0))

参见 https://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html (由 Paul Murrell 撰写)用于解释设备区域。不幸的是我还不能发布图片,否则我也会在这里包含一些例子。

其实我也是来自这个帖子:Align text to a plot with variable size in R我很想看到它包含在 metafor 的 forest() 函数中。 Wolfgang,我会给你发一封 PM。

关于r - R 图中行的恒定绝对间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25742400/

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