Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Allow sorting of labels? #201

Open
z3tt opened this issue Jul 14, 2021 · 1 comment
Open

Feature Request: Allow sorting of labels? #201

z3tt opened this issue Jul 14, 2021 · 1 comment

Comments

@z3tt
Copy link

z3tt commented Jul 14, 2021

Summary

It would be nice to have control of the ordering of labels.

Minimal code example

Related to issue #200 that I filed today. It would be neat to order the boxes in decreasing or ascending order from left to right, bottom to top. I.e. now the order depends on the seed but would it be possible to have a logical order?

library(dplyr)
library(ggplot2)
library(ggrepel)

set.seed(42)

mpg %>% 
  group_by(manufacturer, model) %>% 
  summarize(cty = mean(cty, na.rm = TRUE)) %>% 
  ggplot(aes(x = cty, y = 1, 
             label = paste0(manufacturer, "\n", model))) +
  geom_point(alpha = .3) +
  geom_label_repel(
    fill = "grey90",
    nudge_y      = .03,
    direction    = "y",
    hjust        = .5,
    max.iter = 1e4, max.time = 1,
    size = 1, ## tiny for reprex
    lineheight = .9
  ) +
  xlim(5, 30) +
  ylim(1, 0.95) +
  theme_void()
#> `summarise()` has grouped output by 'manufacturer'. You can override using the `.groups` argument.
#> Warning: ggrepel: 13 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

Created on 2021-07-14 by the reprex package (v1.0.0)

A potentially more insightful example is this one. Here I would like to order the boxes from top to bottom in the same order as they appear in time (along the x axis). Basically, every time ggrepel needs to place a label in a lower row, please you first the remaining one with the lowest x value before trying one with a higher x value.

example_timeline

Suggestions

Not sure it easy to solve since the boxes are placed in random order (I assume), one by one. But that could be also the point to start with. First place box 1, afterwards box 2 and so on.

Version information

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    
system code page: 65001

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        jquerylib_0.1.3   bslib_0.2.4       pillar_1.6.0     
 [5] compiler_4.0.2    highr_0.8         tools_4.0.2       digest_0.6.27    
 [9] jsonlite_1.7.2    evaluate_0.14     lifecycle_1.0.0   tibble_3.1.0     
[13] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.10      reprex_1.0.0     
[17] DBI_1.1.1         cli_2.3.1         rstudioapi_0.13   ggrepel_0.9.1    
[21] yaml_2.2.1        xfun_0.22         dplyr_1.0.5       knitr_1.31       
[25] sass_0.3.1        generics_0.1.0    vctrs_0.3.6       fs_1.5.0         
[29] grid_4.0.2        tidyselect_1.1.0  glue_1.4.2        R6_2.5.0         
[33] processx_3.4.5    fansi_0.4.2       rmarkdown_2.9     callr_3.5.1      
[37] ggplot2_3.3.3     purrr_0.3.4       clipr_0.7.1       magrittr_2.0.1   
[41] ps_1.5.0          htmltools_0.5.1.1 scales_1.1.1      ellipsis_0.3.1   
[45] assertthat_0.2.1  colorspace_2.0-0  utf8_1.2.1        munsell_0.5.0    
[49] crayon_1.4.1  
@aphalo
Copy link
Contributor

aphalo commented Nov 1, 2021

This can be solved using nudging. Function positiion_nudge_to() from package ggpp solves this problem. It takes a vector of nudgings, so by manually or programatically computing these distances you can move the initial positions for the repulsion, or the final position if you set iterations to zero. NOTE: This requires ggrepel under development and ggpp (>= 0.4.2) from CRAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants