forked from vqv/ggbiplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Draw labeled vectors in a ggplot scene | ||
#' | ||
#' @param x,y coordinates of vector ends | ||
#' @param label text labels for vector ends | ||
#' @param scale scale factor for vectors | ||
#' @param origin origin of vectors | ||
#' @param arrow_style style for the vector arrows | ||
#' @param color colors for vectors | ||
#' @param linewidth linewidth for vectors | ||
#' @param adjust adjustment offset for labels | ||
#' @param size text size for labels | ||
#' | ||
#' @return | ||
#' @export | ||
#' | ||
#' @examples | ||
ggvector <- function(x, y, label, | ||
scale = 1, | ||
origin = c(0, 0), | ||
arrow_style, | ||
color, | ||
linewidth = 1.4, | ||
adjust = 1.25, | ||
size = 3 | ||
){ | ||
|
||
if(missing(arrow_style)) arrow_style <- arrow(length = unit(1/2, 'picas'), | ||
type="closed", | ||
angle=15) | ||
# Variables for text label placement | ||
df <- data.frame(x = x, y = y, label = label) | ||
df$angle <- with(df, (180/pi) * atan(y / x)) | ||
df$hjust <- with(df, (1 - adjust * sign(x)) / 2) | ||
|
||
geom_segment(data = df, | ||
aes(x = origin[1], y = origin[2], | ||
xend = x, yend = y), | ||
arrow = arrow_style, | ||
color = color, | ||
linewidth = linewidth) + | ||
geom_text(data = df, | ||
aes(label = label, x = x, y = y, | ||
angle = angle, hjust = hjust), | ||
color = color, size = size) | ||
|
||
} |