我正在尝试使用模拟退火算法解决工程图的自动标签放置问题。我在 R 的库中遇到了该pointLabel()函数maptools。但是,我发现虽然该函数能够以最佳方式放置标签,但它并没有考虑到底层绘图。我不希望标签与绘图线重叠。有什么方法可以实现吗?此外,我的标签在宽度和高度方面大小不一。除了标签的原点之外,有什么方法可以指定每个标签的尺寸吗?
我不知道如何上传必要的 csv 文件来复制我的工作,但这里是保管箱链接https://www.dropbox.com/s/85qqs4nlvm4crck/TextBoxData.rar?dl=0到它,这是我的代码:
library(dplyr)
library(maptools)
#Known Size of Sheet
xmin <- -357.7
xmax <- 19.3
ymin <- -90.2
ymax <- 159.7
#Read in the data
linescoords <- read.csv("Linecoords.csv")
#Coordinates translation
linescoords$X <- linescoords$X - xmin
linescoords$Y <- linescoords$Y - ymin
#Plot the figure
xlimit <- c(-10, abs(xmin-xmax)+10 )
ylimit <- c(-10, abs(ymin-ymax)+10 )
plot(linescoords$X, linescoords$Y, "p", xlim=xlimit, ylim = ylimit)
i <- seq(1, nrow(linescoords), 2)
for(x in i){
segments(linescoords$X[x], linescoords$Y[x],
linescoords$X[x+1], linescoords$Y[x+1])
}
#Read in text labels data
labels <- read.csv("labels.csv")
#Text labels Coordinates translation
labels$x <- labels$x - xmin
labels$y <- labels$y - ymin
labels$xminl <- labels$xminl - xmin
labels$xmaxl <- labels$xmaxl - xmin
labels$yminl <- labels$yminl - ymin
labels$ymaxl <- labels$ymaxl - ymin
#Point of Origin for labels
points(labels$x, labels$y, pch=16, col="red")
#Width & Height of labels
labels <- labels %>% mutate(width=abs(xminl-xmaxl), height=abs(yminl-ymaxl))
pointLabel(labels$x, labels$y, labels$Point)
任何见解都会有所帮助。谢谢。