Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature request: repel from geom_segment #206

@ekatko1

Description

@ekatko1

Summary

Thanks for the great package!

Currently, the repel works to repel text away from single points. However, it would be great if it could repel text away from segments as well. This would be especially useful for automatically generating nice biplots for multivariate analysis (PCA, RDA, etc.).

Minimal code example

Here is the minimum amount of code needed to demonstrate the issue:

library(ggplot2)
library(ggrepel)

## Example data frame where each species' principal components have been computed.
df = data.frame(
  Species = paste("Species",1:5),
  PC1     = c(-4, -3.5, 1, 2, 3),
  PC2     = c(-1, -1, 0, -0.5, 0.7)) 

## We plot the two primary principal componenets and add species labels
ggplot(df, aes(x=PC1, y = PC2, label=Species)) +
  geom_segment(aes(x=0, y=0, xend=PC1, yend=PC2), 
               arrow = arrow(length = unit(0.1, "inches"))) +
  geom_text_repel() +
  xlim(-5, 5) +
  ylim(-2, 2) +
  # Stadard settings for displaying biplots
  geom_hline(aes(yintercept = 0), size=.2) +
  geom_vline(aes(xintercept = 0), size=.2) +
  coord_fixed()

Here is an image of the output produced by the code:

image

Suggestions

The text should be repelled in the direction of the arrow with which it is associated. If the geom_text_repel() function could take as input the aesthetics xend and yend. If these aesthetics are supplied, the text is placed at coordinates yend and xend and some invisible points along the segment are added to the plot in order to repel the text away from the line. An additional option force_line would also be useful to determine the force with which the lines repel the text.

Version information

Here is the output from sessionInfo() in my R session:

R version 3.6.3 (2020-02-29)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252   
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

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

other attached packages:
[1] ggrepel_0.8.2      ggplot2_3.3.0.9000

loaded via a namespace (and not attached):
 [1] vctrs_0.3.1       fansi_0.4.1       digest_0.6.25     htmltools_0.5.1.1
 [5] R6_2.4.1          scales_1.1.0      assertthat_0.2.1  tinytex_0.21     
 [9] grid_3.6.3        knitr_1.31.5      cli_2.0.2         tidyselect_1.1.0 
[13] generics_0.0.2    munsell_0.5.0     pillar_1.4.3      pander_0.6.4     
[17] compiler_3.6.3    tibble_3.0.0      pkgconfig_2.0.3   labeling_0.3     
[21] purrr_0.3.3       rstudioapi_0.11   glue_1.4.0        xfun_0.20        
[25] magrittr_1.5      farver_2.0.3      rmarkdown_2.6     evaluate_0.14    
[29] gtable_0.3.0      rlang_0.4.10      colorspace_1.4-1  yaml_2.2.1       
[33] tools_3.6.3       lifecycle_0.2.0   ellipsis_0.3.0    dplyr_1.0.0      
[37] withr_2.1.2       crayon_1.3.4      Rcpp_1.0.4.6    ```

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions