【发布时间】:2014-10-27 04:20:09
【问题描述】:
我想在单色图形的一条线上绘制一个标签。所以我需要在标签的每个字母上加上白色的小边框。
文本标签的矩形边框或背景没有用,因为它隐藏了很多绘制的数据。
有没有办法在 R 图中的文本标签周围放置边框、阴影或缓冲区?
shadowtext <- function(x, y=NULL, labels, col='white', bg='black',
theta= seq(pi/4, 2*pi, length.out=8), r=0.1, ... ) {
xy <- xy.coords(x,y)
xo <- r*strwidth('x')
yo <- r*strheight('x')
for (i in theta) {
text( xy$x + cos(i)*xo, xy$y + sin(i)*yo, labels, col=bg, ... )
}
text(xy$x, xy$y, labels, col=col, ... )
}
pdf(file="test.pdf", width=2, height=2); par(mar=c(0,0,0,0)+.1)
plot(c(0,1), c(0,1), type="l", lwd=20, axes=FALSE, xlab="", ylab="")
text(1/6, 1/6, "Test 1")
text(2/6, 2/6, "Test 2", col="white")
shadowtext(3/6, 3/6, "Test 3")
shadowtext(4/6, 4/6, "Test 4", col="black", bg="white")
shadowtext(5/6, 5/6, "Test 5", col="black", bg="white", theta = seq(pi/4, 2*pi, length.out=24))
dev.off()
上面的代码使用the solution from koekenbakker。这对于 PNG 图形来说很好,但是对于高分辨率 PDF,我需要一种不同的方法。
【问题讨论】:
-
R 中有许多不同的绘图功能和图形系统。请更具体地说明您当前使用的绘图命令。更好的是,包括reproducible example。
-
pdf 有什么问题?你是说分辨率?您可以调整 theta 参数以获得更高的分辨率。我会在我的回答中改变这一点。
-
你是对的。在我的示例中,我尝试了 24 个项目的 theta,但还不够。但是在您的示例中,50 可以完美运行。谢谢