Dynamic position for ggplot2 objects (especially geom_text)?

后端 未结 4 1599
半阙折子戏
半阙折子戏 2020-12-13 20:52

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 t

相关标签:
4条回答
  • 2020-12-13 21:08

    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.

    Figure is taken from this blog post.

    0 讨论(0)
  • 2020-12-13 21:13

    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.

    0 讨论(0)
  • 2020-12-13 21:21

    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

    0 讨论(0)
  • 2020-12-13 21:26

    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()
    
    0 讨论(0)
提交回复
热议问题