R绘图:有没有办法在文本标签周围绘制边框、阴影或缓冲区?

R绘图:有没有办法在文本标签周围绘制边框、阴影或缓冲区?,r,plot,text,label,R,Plot,Text,Label,我想在单色图形的一条线上绘制一个标签。所以我需要在标签的每个字母上加上白色的小边框 文本标签矩形的边框或背景没有用处,因为它隐藏了大量打印数据 有没有办法在R图中的文本标签周围放置边框、阴影或缓冲区 shadowtext <- function(x, y=NULL, labels, col='white', bg='black', theta= seq(pi/4, 2*pi, length.out=8), r=0.1, ... ) { xy &l

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

文本标签矩形的边框或背景没有用处,因为它隐藏了大量打印数据

有没有办法在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()

shadowtext您可以尝试此“shadowtext”功能,通过以不同颜色稍微偏移几次打印文本,在文本周围绘制光晕或边框。全部归功于


shadowtext我为文本字段编写了一个类似的函数,它也适用于对数刻度

install.packages("berryFunctions")
library("berryFunctions")
?textField
这可能被认为是更好的矢量图形。 以下是一些例子:


PS:如果您想参与:

以下是我在标记轮廓(或本例中的圆)时添加文本缓冲区的尝试。它肯定没有我想象的那么有效或漂亮,但它达到了我将文本标签与行隔离的目的

require(shape)
plot(250,250, xlim=c(0,500), ylim=c(0,500), axes=F, ylab="", xlab="", col="black") ## Set up the plotting area

circle_radius<-c(200, 150, 50) ## We want to draw a few circles which we will label after using our buffer

for (i in 1:length(circle_radius)){
    plotcircle(mid=c(250,250), r=circle_radius[i], lwd=0.5,lcol="grey40") ## Iteratively plot the circles

text_buffer<-seq(0.1, 0.7, by=0.01) ## This is the key to the text buffer. Create a vector of values to set the character size (cex) during the following loop

for(j in text_buffer){
    text(x=250+circle_radius[i], 250, print(paste(circle_radius[i],"m")), cex=j, srt=270, col="white") ## Write the text buffer in white starting from the smallest value of cex to the largest
}  ## End of loop for writing buffer
    text(x=250+circle_radius[i], 250, print(paste(circle_radius[i],"m")), cex=0.5, srt=270) ## Write over the buffer with black text

}  ## End of loop for drawing circles
require(形状)
绘图(250250,xlim=c(0500),ylim=c(0500),轴=F,ylab=“”,xlab=“”,col=“黑色”)#设置绘图区域

circle_radius我需要对R中的地图执行此操作,并最终使用“光栅”包在文本标签周围绘制光晕

比如说,

library(raster)

text(Points, labels = Points$Labels, halo = TRUE, hw = 0.08, hc = "white", cex = 0.8)
# hw = halo width
# hc = halo color

该软件包可用于在
ggplot2
绘图的文本周围绘制轮廓或阴影

库(ggplot2)
库(阴影文本)

jet.colors在R中有许多不同的打印功能和图形系统。请更具体地说明您当前使用的打印命令。更好的是,包含一个。pdf有什么问题?你是说决议?您可以调整θ参数以获得更高的分辨率。我会在回答中改变这个,你是对的。在我的例子中,我尝试了24个项目的θ,但这还不够。但在你的例子中,50是完美的。谢谢,太棒了!想知道类似的东西是否可以改编成多行文字。当然,你可以使用xpd=T来修改它,但是如果能轻松地用它在页边空白处写标题和图例,那就太好了!
library(raster)

text(Points, labels = Points$Labels, halo = TRUE, hw = 0.08, hc = "white", cex = 0.8)
# hw = halo width
# hc = halo color