r - 如何在不改变 xy 坐标的情况下在绘图中移动单个文本标签?
问题描述
我想将国家标签(实际上是十个)添加到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")
我宁愿寻找这样的调整解决方案,因为我不喜欢更改坐标,因为它们很好地代表了每个国家的中心。
如何在不更改/坐标moveString
的情况下移动并保持stayString
原位?x
y
解决方案
想到的一个解决方案是创建两个函数,并根据您是否想要offset
它来拆分每个字符串。
我们可以使用、fromtext()
和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")
推荐阅读
- firebase - Firebase / NoSQL - 如何聚合数据以进行统计
- java - R Studio 错误:NoclassDefFoundError (Java):org/apache/poi/openxml4j/exceptions/InvalidFormatException
- angular - 从另一个组件绑定和编辑对象
- asp.net-core - 想要使用 Asp.net Core、EF Core(每个租户单独的数据库)创建多租户应用程序
- bash - 在一行中提取并显示多个字符串
- javascript - 如何在 react native 中使用其他组件更新 app.js 的状态?
- dictionary - 模板参数的数量错误(1,应该是 2)。任何人都可以看看吗?
- unity3d - 如何修复 untiy3d 对象在某些方面显示为白色
- asp.net-mvc - 洋葱架构中存储过程的使用
- oracle11g - oracle中度数符号的垃圾值