R:结合 mtext() 与 points() - 指定坐标的正确方法?
R: combine mtext() with points() - correct way to specify coordinates?
我想将我用 mtext()
放置在页边空白处的文本与我使用 points()
或 polygon()
创建的图形对象组合起来。以下示例使用默认绘图设置大致适用于我:
plot(1)
mtext("This is a red dot:", side=1, line=2, cex=0.8)
par(xpd=T)
points(1.08, 0.512, pch=15, cex=1.5, col="red")
但是,使用 plot(1:10)
代替或在其前面加上 windows(8,8)
会将点放在错误的位置,因为 points()
需要用户坐标。有没有办法让我的点正确放置而不受绘图限制或设备大小的影响?
我想我已经根据@koekenbakker 的建议找到了可行的答案。为了使它看起来漂亮,仍然需要进行一些小的调整以使点与文本一致,但它似乎独立于绘图大小和轴限制而运行良好(但一旦创建就无法调整绘图的大小)。要对点的确切位置进行调整,我建议在此示例中使用 strheight("O", cex=0.8)
和 strheight("O", cex=0.8)
的分数(使用 cex=0.8
进行演示)。
plot(1)
mtext("This is a red dot:", side=1, line=2, cex=0.8, col="green") ## place sample text at bottom of figure
par(xpd=T) ## enable plotting outside plot region
textXPos <- mean(par("usr")[1:2]) ## x position is middle of plot
textYPos <- par("usr")[3] - strheight("O") * 4 ## y position is below bottom of plot (line 2 = 4 * height of letter O)
text(textXPos, textYPos, "This is a red dot:", cex=0.8, col="red", adj=c(0.5, 0)) ## this text overlaps - y position is correct
pointXPos <- textXPos + 0.5 * strwidth("This is a red dot:", cex=0.8) ## text is centred, so need to move half of the text width to the right
points(pointXPos, textYPos, pch=16, col="red") ## still needs minor adjustments to x and y position to make it look nice
我想将我用 mtext()
放置在页边空白处的文本与我使用 points()
或 polygon()
创建的图形对象组合起来。以下示例使用默认绘图设置大致适用于我:
plot(1)
mtext("This is a red dot:", side=1, line=2, cex=0.8)
par(xpd=T)
points(1.08, 0.512, pch=15, cex=1.5, col="red")
但是,使用 plot(1:10)
代替或在其前面加上 windows(8,8)
会将点放在错误的位置,因为 points()
需要用户坐标。有没有办法让我的点正确放置而不受绘图限制或设备大小的影响?
我想我已经根据@koekenbakker 的建议找到了可行的答案。为了使它看起来漂亮,仍然需要进行一些小的调整以使点与文本一致,但它似乎独立于绘图大小和轴限制而运行良好(但一旦创建就无法调整绘图的大小)。要对点的确切位置进行调整,我建议在此示例中使用 strheight("O", cex=0.8)
和 strheight("O", cex=0.8)
的分数(使用 cex=0.8
进行演示)。
plot(1)
mtext("This is a red dot:", side=1, line=2, cex=0.8, col="green") ## place sample text at bottom of figure
par(xpd=T) ## enable plotting outside plot region
textXPos <- mean(par("usr")[1:2]) ## x position is middle of plot
textYPos <- par("usr")[3] - strheight("O") * 4 ## y position is below bottom of plot (line 2 = 4 * height of letter O)
text(textXPos, textYPos, "This is a red dot:", cex=0.8, col="red", adj=c(0.5, 0)) ## this text overlaps - y position is correct
pointXPos <- textXPos + 0.5 * strwidth("This is a red dot:", cex=0.8) ## text is centred, so need to move half of the text width to the right
points(pointXPos, textYPos, pch=16, col="red") ## still needs minor adjustments to x and y position to make it look nice