如何移动绘图中的单个文本标签 w/o 改变 x-y 坐标?
How to move a single text label in a plot w/o changing x-y-coordinates?
我想为 rworldmap
添加国家/地区标签(实际上是十个)。其中两个重叠,因为它们是小的、毗邻的州。我想将其中一个稍微移到一边,但要保留第一个。
我认为我不需要在此处显示 rworldmap
代码,因为我可以将问题分解为 text
函数。
来自函数的参数默认值
text(x, y = NULL, labels = seq_along(x$x), adj = NULL,
pos = NULL, offset = 0.5, vfont = NULL,
cex = 1, col = NULL, font = NULL, ...)
我会得出默认的 pos
是 NULL
,所以我说 pos=c(NULL, 4)
。然而,这并没有像预期的那样工作;第一个标签也被移动。 moveString
已正确移动,但另一个应留在原处。我已经尝试了所有可用的 pos
为 stayString
,但它们与原始位置不对应。我也试过 adj
但没有成功。
plot(0:3, type="n")
grid()
text(c(2, 2.2), rep(3, 2), c("stayString", "moveString"),
col="black") # raw
text(c(2, 2.2), rep(2.5, 2), c("stayString", "moveString"),
pos=c(NULL, 4), col="red") # unexpected result
# other attempts
text(c(2, 2.2), rep(2, 2), c("stayString", "moveString"),
pos=c(1, 4), col="green")
text(c(2, 2.2), rep(1.5, 2), c("stayString", "moveString"),
adj=c(.5, 1), col="blue")
text(c(2, 2.2), rep(1, 2), c("stayString", "moveString"),
pos=c(2, 4), col="purple")
text(c(2, 2.2), rep(.5, 2), c("stayString", "moveString"),
pos=c(1, 4), adj=c(.5, 1), col="orange")
我宁愿寻找这样的调整解决方案,因为我不喜欢更改坐标,因为它们很好地代表了每个国家/地区的中心。
如何在不改变 x
/y
坐标的情况下移动 moveString
并保持 stayString
的位置?
想到的一个解决方案是创建两个函数,并根据您是否想要 offset
拆分每个字符串。
我们可以使用 text()
、pos
和 offset
来稍微移动文本。
text_stay <- function(x, y, lab, ...) {
text(x,y, labels=lab, ...)
}
text_move <- function(x,y,lab, p=4, off=2, ...) {
text(x, y, labels=lab, pos=p, offset=off, ...)
}
例如:
plot(0:3, type="n")
grid()
# split the text and use the appropriate wrapper function
text_stay(rep(2, 3), 1:3, "stay", col="red")
text_move(rep(2, 3), 1:3, "move", col = "blue")
我想为 rworldmap
添加国家/地区标签(实际上是十个)。其中两个重叠,因为它们是小的、毗邻的州。我想将其中一个稍微移到一边,但要保留第一个。
我认为我不需要在此处显示 rworldmap
代码,因为我可以将问题分解为 text
函数。
来自函数的参数默认值
text(x, y = NULL, labels = seq_along(x$x), adj = NULL,
pos = NULL, offset = 0.5, vfont = NULL,
cex = 1, col = NULL, font = NULL, ...)
我会得出默认的 pos
是 NULL
,所以我说 pos=c(NULL, 4)
。然而,这并没有像预期的那样工作;第一个标签也被移动。 moveString
已正确移动,但另一个应留在原处。我已经尝试了所有可用的 pos
为 stayString
,但它们与原始位置不对应。我也试过 adj
但没有成功。
plot(0:3, type="n")
grid()
text(c(2, 2.2), rep(3, 2), c("stayString", "moveString"),
col="black") # raw
text(c(2, 2.2), rep(2.5, 2), c("stayString", "moveString"),
pos=c(NULL, 4), col="red") # unexpected result
# other attempts
text(c(2, 2.2), rep(2, 2), c("stayString", "moveString"),
pos=c(1, 4), col="green")
text(c(2, 2.2), rep(1.5, 2), c("stayString", "moveString"),
adj=c(.5, 1), col="blue")
text(c(2, 2.2), rep(1, 2), c("stayString", "moveString"),
pos=c(2, 4), col="purple")
text(c(2, 2.2), rep(.5, 2), c("stayString", "moveString"),
pos=c(1, 4), adj=c(.5, 1), col="orange")
我宁愿寻找这样的调整解决方案,因为我不喜欢更改坐标,因为它们很好地代表了每个国家/地区的中心。
如何在不改变 x
/y
坐标的情况下移动 moveString
并保持 stayString
的位置?
想到的一个解决方案是创建两个函数,并根据您是否想要 offset
拆分每个字符串。
我们可以使用 text()
、pos
和 offset
来稍微移动文本。
text_stay <- function(x, y, lab, ...) {
text(x,y, labels=lab, ...)
}
text_move <- function(x,y,lab, p=4, off=2, ...) {
text(x, y, labels=lab, pos=p, offset=off, ...)
}
例如:
plot(0:3, type="n")
grid()
# split the text and use the appropriate wrapper function
text_stay(rep(2, 3), 1:3, "stay", col="red")
text_move(rep(2, 3), 1:3, "move", col = "blue")