Open ekatko1 opened 3 years ago
Thanks for the request! I am accepting pull requests 👍
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)
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:
Here is an image of the output produced by the code:
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 aestheticsxend
andyend
. If these aesthetics are supplied, the text is placed at coordinatesyend
andxend
and some invisible points along the segment are added to the plot in order to repel the text away from the line. An additional optionforce_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: