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: repel from geom_segment #206

Open
ekatko1 opened this issue Sep 21, 2021 · 2 comments
Open

Feature request: repel from geom_segment #206

ekatko1 opened this issue Sep 21, 2021 · 2 comments

Comments

@ekatko1
Copy link

ekatko1 commented Sep 21, 2021

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    ```
@slowkow
Copy link
Owner

slowkow commented Sep 21, 2021

Thanks for the request! I am accepting pull requests 👍

@aphalo
Copy link
Contributor

aphalo commented Nov 1, 2021

At least for this simple example adding radial nudging away from the origin gives the desired result. NOTE: This is using ggrepel under development and ggpp (>= 0.4.2) from CRAN.

library(ggplot2)
library(ggrepel)
library(ggpp)
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

## 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(position = position_nudge_center(0.2, 0.1, 0, 0)) +
  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()

Created on 2021-11-01 by the reprex package (v2.0.1)

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

No branches or pull requests

3 participants