... | ... |
@@ -1,33 +1,38 @@ |
1 |
-#' Function for biplotting with no point labels and with points color-coded according |
|
2 |
-#' to a quantitative variable. For example: the rank of normalization performance. |
|
3 |
-#' |
|
1 |
+#' Function for biplotting with no point labels and with |
|
2 |
+#' points color-coded according to a quantitative variable. |
|
3 |
+#' For example: the rank of normalization performance. |
|
4 |
+#' |
|
4 | 5 |
#' This function implements biplot for \code{\link[stats]{prcomp}} objects. |
5 |
-#' |
|
6 |
+#' |
|
6 | 7 |
#' @param x \code{\link[stats]{prcomp}} object. |
7 |
-#' @param y numeric. Quantitative values used to color the points. |
|
8 |
-#' If rank is FALSE, all values must be positive integers and less than or equal to the length of y. |
|
9 |
-#' @param rank logical. If TRUE (default) y will be transformed by the rank() function |
|
8 |
+#' @param y numeric. Quantitative values used to color the points. If rank is |
|
9 |
+#' FALSE, all values must be positive integers and less than or equal to the |
|
10 |
+#' length of y. |
|
11 |
+#' @param rank logical. If TRUE (default) y will be transformed by the rank() |
|
12 |
+#' function |
|
10 | 13 |
#' @param ties_method character. ties.method used by the rank() function |
11 |
-#' @param choices numeric. 2 principal components to plot. Default to first two PCs. |
|
12 |
-#' @param expand numeric. value used to adjust the spread of the arrows relative |
|
13 |
-#' to the points. |
|
14 |
+#' @param choices numeric. 2 principal components to plot. Default to first two |
|
15 |
+#' PCs. |
|
16 |
+#' @param expand numeric. value used to adjust the spread of the arrows |
|
17 |
+#' relative to the points. |
|
14 | 18 |
#' @param ... arguments passed to plot. |
15 |
-#' |
|
19 |
+#' |
|
16 | 20 |
#' @importFrom grDevices colorRampPalette |
17 | 21 |
#' @export |
18 | 22 |
#' |
19 | 23 |
#' @return Invisibly returns scaled point coordinates used in plot. |
20 |
-#' |
|
24 |
+#' |
|
21 | 25 |
#' @examples |
22 | 26 |
#' mat <- matrix(rnorm(1000), ncol=10) |
23 | 27 |
#' colnames(mat) <- paste("X", 1:ncol(mat), sep="") |
24 |
-#' |
|
28 |
+#' |
|
25 | 29 |
#' pc <- prcomp(mat) |
26 |
-#' |
|
30 |
+#' |
|
27 | 31 |
#' biplot_color(pc, rank(pc$x[,1])) |
28 |
-#' |
|
32 |
+#' |
|
29 | 33 |
biplot_color <- function(x, y, rank = TRUE, |
30 |
- ties_method = c("max", "min", "first", "last", "random"), |
|
34 |
+ ties_method = c("max", "min", |
|
35 |
+ "first", "last", "random"), |
|
31 | 36 |
choices = 1:2, expand = 1, ...) { |
32 | 37 |
|
33 | 38 |
if(rank){ |
... | ... |
@@ -1,27 +1,22 @@ |
1 |
-## When porting the package to S4, we can make this a biplot method |
|
2 |
- |
|
3 |
-#' Another implementation of the biplot function |
|
4 |
-#' |
|
5 |
-#' Function to plot a biplot with no point labels and with points color-coded |
|
6 |
-#' according to a certain quantitative variable, for instance the rank of the |
|
7 |
-#' normalization performance or the expression of a certain gene. |
|
1 |
+#' Function for biplotting with no point labels and with points color-coded according |
|
2 |
+#' to a quantitative variable. For example: the rank of normalization performance. |
|
8 | 3 |
#' |
9 |
-#' This function implements the biplot only for \code{\link[stats]{prcomp}} |
|
10 |
-#' objects. Eventually, we will turn this into an S4 method. |
|
4 |
+#' This function implements biplot for \code{\link[stats]{prcomp}} objects. |
|
11 | 5 |
#' |
12 |
-#' @param x the result of a call to \code{\link[stats]{prcomp}}. |
|
13 |
-#' @param y an array of values used to color the points. |
|
6 |
+#' @param x \code{\link[stats]{prcomp}} object. |
|
7 |
+#' @param y numeric. Quantitative values used to color the points. |
|
14 | 8 |
#' If rank is FALSE, all values must be positive integers and less than or equal to the length of y. |
15 | 9 |
#' @param rank logical. If TRUE (default) y will be transformed by the rank() function |
16 | 10 |
#' @param ties_method character. ties.method used by the rank() function |
17 |
-#' @param choices which principal components to plot. Only 2D plots are |
|
18 |
-#' possible for now. Default to first two PCs. |
|
19 |
-#' @param expand numeric value used to adjust the spread of the arrows relative |
|
11 |
+#' @param choices numeric. 2 principal components to plot. Default to first two PCs. |
|
12 |
+#' @param expand numeric. value used to adjust the spread of the arrows relative |
|
20 | 13 |
#' to the points. |
21 |
-#' @param ... passed to plot. |
|
14 |
+#' @param ... arguments passed to plot. |
|
22 | 15 |
#' |
23 | 16 |
#' @importFrom grDevices colorRampPalette |
24 | 17 |
#' @export |
18 |
+#' |
|
19 |
+#' @return Invisibly returns scaled point coordinates used in plot. |
|
25 | 20 |
#' |
26 | 21 |
#' @examples |
27 | 22 |
#' mat <- matrix(rnorm(1000), ncol=10) |
... | ... |
@@ -29,9 +24,11 @@ |
29 | 24 |
#' |
30 | 25 |
#' pc <- prcomp(mat) |
31 | 26 |
#' |
32 |
-#' biplot_colored(pc, rank(pc$x[,1])) |
|
27 |
+#' biplot_color(pc, rank(pc$x[,1])) |
|
33 | 28 |
#' |
34 |
-biplot_colored <- function(x, y, rank = TRUE, ties_method = c("max", "min", "first", "last", "random"), choices=1:2, expand=1, ...) { |
|
29 |
+biplot_color <- function(x, y, rank = TRUE, |
|
30 |
+ ties_method = c("max", "min", "first", "last", "random"), |
|
31 |
+ choices = 1:2, expand = 1, ...) { |
|
35 | 32 |
|
36 | 33 |
if(rank){ |
37 | 34 |
|
... | ... |
@@ -63,7 +60,9 @@ biplot_colored <- function(x, y, rank = TRUE, ties_method = c("max", "min", "fir |
63 | 60 |
|
64 | 61 |
ratio <- max(range(yy)/range(xx))/expand |
65 | 62 |
|
66 |
- cols <- rev(colorRampPalette(c("black","navyblue","mediumblue","dodgerblue3","aquamarine4","green4","yellowgreen","yellow"))(length(y)))[y] |
|
63 |
+ cols <- rev(colorRampPalette(c("black","navyblue","mediumblue", |
|
64 |
+ "dodgerblue3","aquamarine4","green4", |
|
65 |
+ "yellowgreen","yellow"))(length(y)))[y] |
|
67 | 66 |
plot(xx, pch=19, col=cols, ...) |
68 | 67 |
|
69 | 68 |
labs <- rownames(yy) |
... | ... |
@@ -10,7 +10,10 @@ |
10 | 10 |
#' objects. Eventually, we will turn this into an S4 method. |
11 | 11 |
#' |
12 | 12 |
#' @param x the result of a call to \code{\link[stats]{prcomp}}. |
13 |
-#' @param y the rank value that should be used to color the points. |
|
13 |
+#' @param y an array of values used to color the points. |
|
14 |
+#' If rank is FALSE, all values must be positive integers and less than or equal to the length of y. |
|
15 |
+#' @param rank logical. If TRUE (default) y will be transformed by the rank() function |
|
16 |
+#' @param ties_method character. ties.method used by the rank() function |
|
14 | 17 |
#' @param choices which principal components to plot. Only 2D plots are |
15 | 18 |
#' possible for now. Default to first two PCs. |
16 | 19 |
#' @param expand numeric value used to adjust the spread of the arrows relative |
... | ... |
@@ -28,8 +31,29 @@ |
28 | 31 |
#' |
29 | 32 |
#' biplot_colored(pc, rank(pc$x[,1])) |
30 | 33 |
#' |
31 |
-biplot_colored <- function(x, y, choices=1:2, expand=1, ...) { |
|
34 |
+biplot_colored <- function(x, y, rank = TRUE, ties_method = c("max", "min", "first", "last", "random"), choices=1:2, expand=1, ...) { |
|
32 | 35 |
|
36 |
+ if(rank){ |
|
37 |
+ |
|
38 |
+ ties_method <- match.arg(ties_method) |
|
39 |
+ y = rank(y,ties.method = ties_method) |
|
40 |
+ |
|
41 |
+ }else{ |
|
42 |
+ |
|
43 |
+ if(any(abs(y - round(y)) > .Machine$double.eps^0.5)){ |
|
44 |
+ stop("ranks must be integer") |
|
45 |
+ }else{y = as.integer(y)} |
|
46 |
+ |
|
47 |
+ if(any(y <= 0)){ |
|
48 |
+ stop("ranks must be positive") |
|
49 |
+ } |
|
50 |
+ |
|
51 |
+ if(any(y > length(y))){ |
|
52 |
+ stop("ranks must be less than or equal to total number of elements") |
|
53 |
+ } |
|
54 |
+ |
|
55 |
+ } |
|
56 |
+ |
|
33 | 57 |
lam <- x$sdev[choices] |
34 | 58 |
n <- NROW(x$x) |
35 | 59 |
lam <- lam * sqrt(n) |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,49 @@ |
1 |
+## When porting the package to S4, we can make this a biplot method |
|
2 |
+ |
|
3 |
+#' Another implementation of the biplot function |
|
4 |
+#' |
|
5 |
+#' Function to plot a biplot with no point labels and with points color-coded |
|
6 |
+#' according to a certain quantitative variable, for instance the rank of the |
|
7 |
+#' normalization performance or the expression of a certain gene. |
|
8 |
+#' |
|
9 |
+#' This function implements the biplot only for \code{\link[stats]{prcomp}} |
|
10 |
+#' objects. Eventually, we will turn this into an S4 method. |
|
11 |
+#' |
|
12 |
+#' @param x the result of a call to \code{\link[stats]{prcomp}}. |
|
13 |
+#' @param y the rank value that should be used to color the points. |
|
14 |
+#' @param choices which principal components to plot. Only 2D plots are |
|
15 |
+#' possible for now. Default to first two PCs. |
|
16 |
+#' @param expand numeric value used to adjust the spread of the arrows relative |
|
17 |
+#' to the points. |
|
18 |
+#' @param ... passed to plot. |
|
19 |
+#' |
|
20 |
+#' @importFrom grDevices colorRampPalette |
|
21 |
+#' @export |
|
22 |
+#' |
|
23 |
+#' @examples |
|
24 |
+#' mat <- matrix(rnorm(1000), ncol=10) |
|
25 |
+#' colnames(mat) <- paste("X", 1:ncol(mat), sep="") |
|
26 |
+#' |
|
27 |
+#' pc <- prcomp(mat) |
|
28 |
+#' |
|
29 |
+#' biplot_colored(pc, rank(pc$x[,1])) |
|
30 |
+#' |
|
31 |
+biplot_colored <- function(x, y, choices=1:2, expand=1, ...) { |
|
32 |
+ |
|
33 |
+ lam <- x$sdev[choices] |
|
34 |
+ n <- NROW(x$x) |
|
35 |
+ lam <- lam * sqrt(n) |
|
36 |
+ |
|
37 |
+ xx <- t(t(x$x[, choices])/lam) |
|
38 |
+ yy <- t(t(x$rotation[, choices]) * lam) |
|
39 |
+ |
|
40 |
+ ratio <- max(range(yy)/range(xx))/expand |
|
41 |
+ |
|
42 |
+ cols <- rev(colorRampPalette(c("black","navyblue","mediumblue","dodgerblue3","aquamarine4","green4","yellowgreen","yellow"))(length(y)))[y] |
|
43 |
+ plot(xx, pch=19, col=cols, ...) |
|
44 |
+ |
|
45 |
+ labs <- rownames(yy) |
|
46 |
+ |
|
47 |
+ text(yy/ratio, labels=labs, col=2) |
|
48 |
+ arrows(0, 0, yy[, 1] * 0.8/ratio, yy[, 2] * 0.8/ratio, col = 2, length = 0.1) |
|
49 |
+} |