gpt4 book ai didi

R 在文本标签周围绘制 : Is there a way to draw a border, 阴影或缓冲区?

转载 作者:行者123 更新时间:2023-12-02 23:21:35 26 4
gpt4 key购买 nike

我想在单色图形的线上绘制标签。所以我需要在标签的每个字母上加上白色的小边框。

文本标签的矩形边框或背景没有用,因为它隐藏了很多绘制的数据。

有没有办法在 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,我需要不同的方法。

最佳答案

您可以尝试这个“shadowtext”功能,通过以不同颜色稍微偏移打印几次,在文本周围绘制光环或边框。所有功劳归于Greg Snow here .

shadowtext <- function(x, y=NULL, labels, col='white', bg='black', 
theta= seq(0, 2*pi, length.out=50), r=0.1, ... ) {

xy <- xy.coords(x,y)
xo <- r*strwidth('A')
yo <- r*strheight('A')

# draw background text with small shift in x and y in background colour
for (i in theta) {
text( xy$x + cos(i)*xo, xy$y + sin(i)*yo, labels, col=bg, ... )
}
# draw actual text in exact xy position in foreground colour
text(xy$x, xy$y, labels, col=col, ... )
}

# And here is an example of use:
# pdf(file="test2.pdf", width=2, height=2); par(mar=c(0,0,0,0)+.1)
plot(c(0,1), c(0,1), type="n", lwd=20, axes=FALSE, xlab="", ylab="")

rect(xleft = 0.5, xright = 1, ybottom = 0, ytop = 1, col=1)
text(1/6, 1/6, "Test 1")
shadowtext(2/6, 2/6, "Test 2", col='red', bg="blue")
shadowtext(3/6, 3/6, "Test 3", cex=2)

# `r` controls the width of the border
shadowtext(5/6, 5/6, "Test 4", col="black", bg="white", cex=4, r=0.2)
# dev.off()

enter image description here

关于R 在文本标签周围绘制 : Is there a way to draw a border, 阴影或缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631216/

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