ggplot2 对象的动态位置(尤其是 geom_text)?

发布于 2024-11-29 01:38:13 字数 716 浏览 1 评论 0原文

使用 ArcGIS 制作地图时,该软件默认会自动推送点和多边形标签,以避免使用专有算法发生重叠。他们将此称为动态标签ggplot2 具有 position_jitter,对于点来说非常有用(因为动态标签可能会产生系统偏差),但对于使用 geom_text 的标签来说不太好。

以下是动态标签算法可以解决的一些抖动问题的示例:

library(ggplot2)
ggplot( mtcars,aes( x=wt, y=mpg, label=rownames(mtcars) ) ) +
  geom_point() +
  geom_text( position=position_jitter(h=1,w=1) )

jittered labels with issuesnote

是否执行这样的动态标签ggplot2 中已经存在该功能吗?

如果没有,有哪些算法可以实现这一点?是否可以在 R 中实现 position_dynamic

When using ArcGIS to make maps, the software by default pushes point and polygon labels around automatically to avoid overlap using a proprietary algorithm. They refer to this as dynamic labeling. ggplot2 has position_jitter which is excellent for points (since dynamic labeling might create systematic bias), but less good for labels using geom_text.

Here's an example of some problems with jitter that a dynamic labeling algorithm might solve:

library(ggplot2)
ggplot( mtcars,aes( x=wt, y=mpg, label=rownames(mtcars) ) ) +
  geom_point() +
  geom_text( position=position_jitter(h=1,w=1) )

jittered labels with problems noted

Does such a dynamic labeling feature exist already in ggplot2?

If not, what algorithms exist for doing so and is it possible to implement a position_dynamic in R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅的被狗咬 2024-12-06 01:38:14

AFAIK,现有的最好的是 directlabels,可从 R-forge 获取CRAN 以及全面的 示例页面

这似乎是一个很好的起点,但在我看来有以下负面影响:

  • 与分离数据和表示的 ggplot 哲学不同,directlabels 返回 ggplot 对象而不是 geom
  • 它只适用于group美学,而不适用于个别点

我前段时间浏览过源代码,我认为调整代码来解决我提到的这两点应该相当容易。

这个问题中有一个如何将其与ggplot一起使用的示例

AFAIK, the best that exists is directlabels, available from R-forge and CRAN and with a comprehensive examples page.

This seems a good starting point, but in my opinion has the following negative aspects:

  • Unlike the ggplot philosophy of separating data and presentation, directlabels returns a ggplot object rather than a geom
  • it only works on the group aesthetic, not individual points

I have glanced at the source code some time ago and I think it should be reasonably easy to adapt the code to address both points I mention.

There is an example of how to use this with ggplot in this question on SO

趁微风不噪 2024-12-06 01:38:14

这不是任何可以直接在 ggplot2 中使用的东西,但是 vegan 包中的 ordipointlabel() 函数尝试做类似的事情。它将数据显示为点,并尝试使用适当的标签来标记每个点,使用优化算法将标签放置在其点旁边,但不与其他标签和点重叠。

?ordipointlabel 提到它基于 ma​​ptools 包中的 pointlabel(),这可能是另一个寻找灵感的地方。

This isn't anything that can be used directly in ggplot2, but the ordipointlabel() function in package vegan tries to do something similar. It displays data as points and tries to label each point with the appropriate label, using an optimisation algorithm to position the labels next to their point but without overlapping other labels and points.

?ordipointlabel mentions that it is based on pointlabel() in the maptools package, which could be another place to look for inspiration.

醉城メ夜风 2024-12-06 01:38:13

查看新包 ggrepel
ggrepel 为 ggplot2 提供几何图形来排斥重叠的文本标签。它适用于 geom_text 和 geom_label。

输入图像描述这里

图取自这篇博文

Check out the new package ggrepel.
ggrepel provides geoms for ggplot2 to repel overlapping text labels. It works both for geom_text and geom_label.

enter image description here

Figure is taken from this blog post.

浅笑依然 2024-12-06 01:38:13

我在处理几个绘图时遇到了类似的问题,并编写了一个基本包,该包使用力场模拟来调整对象位置。虽然可以进行很多改进,包括与 ggplot 集成等,但它似乎完成了任务。下图说明了该功能:

install.packages("FField", type = "source")
install.packages("ggplot2")
install.packages("gridExtra")
library(FField)
FFieldPtRepDemo()

I ran into a similar problem with several of the plots I have been working with and wrote a basic package that uses force field simulation to adjust object location. While much improvement is possible, including integration with ggplot, etc. it seems to get the task accomplished. The following illustrates the functionality:

install.packages("FField", type = "source")
install.packages("ggplot2")
install.packages("gridExtra")
library(FField)
FFieldPtRepDemo()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文