Browse code

Updated biplot method and manual.

Jaehyun Joo authored on 16/12/2021 02:41:42
Showing 8 changed files

... ...
@@ -1,9 +1,55 @@
1
+##' Biplot of dimension-reduced data
2
+##'
3
+##' Visualize an overlay of a score plot and a loading plot using the
4
+##' [poplin_reduce] output.
5
+##'
6
+##' @param x A dimension-reduced data matrix produced by [poplin_reduce] or
7
+##'   \linkS4class{poplin} object containing dimension-reduced data.
8
+##' @param y A data matrix for loadings. Not required for the [reduce_pca] and
9
+##'   [reduce_plsda] outputs.
10
+##' @param poplin_in Name of a dimension-reduced data matrix to retrieve.
11
+##' @param comp A numeric vector of length 2 indicating two components to plot.
12
+##' @param group A discrete variable to visualize the grouping structure.
13
+##' @param group_col A vector of colors with the same length of unique values in
14
+##'   \code{group}.
15
+##' @param point_size The size of points.
16
+##' @param point_shape_by_group Logical controlling whether each group have
17
+##'   different shapes of data points. Also can be a numeric vector with the
18
+##'   same length of unique values in \code{group} to manually set point shapes.
19
+##' @param label Logical controlling whether score labels are shown instead of
20
+##'   points.
21
+##' @param label_subset A character vector specifying a subset of score labels
22
+##'   to display.
23
+##' @param ellipse Logical controlling whether data ellipses are shown using
24
+##'   [ggplot2::stat_ellipse].
25
+##' @param xlab The title of x-axis of the plot.
26
+##' @param ylab The title of y-axis of the plot.
27
+##' @param title The main title of the plot.
28
+##' @param legend Logical controlling whether the plot legend is shown.
29
+##' @param arrow_len Numeric value controlling the length of arrow head.
30
+##' @param arrow_alpha Numeric value controlling the transparency of arrow.
31
+##' @param arrow_label Logical controlling whether text labels for arrows are
32
+##'   shown.
33
+##' @param arrow_label_ext Numeric value controlling the scalar extension for
34
+##'   arrow labels.
35
+##' @param arrow_label_size Numeric value controlling the size of arrow labels.
36
+##' @param arrow_label_col Character string indicating the color of arrow
37
+##'   labels.
38
+##' @param arrow_label_subset A character vector specifying a subset of arrow
39
+##'   labels to display.
40
+##' @param ... Arguments passed to the default method.
41
+##' @return A ggplot object.
42
+##' @seealso [poplin_reduce], [poplin_scoreplot].
43
+##' @name poplin_biplot
44
+NULL
45
+
1 46
 ##' @importFrom ggplot2 geom_segment scale_x_continuous scale_y_continuous sec_axis
2 47
 ##' @export
3 48
 poplin_biplot <- function(x, ...) {
4 49
   UseMethod("poplin_biplot")
5 50
 }
6 51
 
52
+##' @rdname poplin_biplot
7 53
 ##' @export
8 54
 poplin_biplot.default <- function(x, y, comp = 1:2, group,
9 55
                                   group_col = NULL,
... ...
@@ -21,14 +67,14 @@ poplin_biplot.default <- function(x, y, comp = 1:2, group,
21 67
                                   arrow_label_col = "red",
22 68
                                   arrow_label_subset = NULL) {
23 69
   p <- poplin_scoreplot(x, comp = comp, group = group,
24
-                   group_col = group_col,
25
-                   point_shape_by_group = point_shape_by_group,
26
-                   point_size = point_size,
27
-                   label = label, label_size = label_size,
28
-                   label_subset = label_subset,
29
-                   ellipse = ellipse,
30
-                   xlab = xlab, ylab = ylab,
31
-                   title = title, legend = legend)
70
+                        group_col = group_col,
71
+                        point_shape_by_group = point_shape_by_group,
72
+                        point_size = point_size,
73
+                        label = label, label_size = label_size,
74
+                        label_subset = label_subset,
75
+                        ellipse = ellipse,
76
+                        xlab = xlab, ylab = ylab,
77
+                        title = title, legend = legend)
32 78
   scalers <- 0.7 * c(
33 79
     (max(x[, comp[1]]) - min(x[, comp[1]])) / (max(y[, comp[1]]) - min(y, comp[1])),
34 80
     (max(x[, comp[2]]) - min(x[, comp[2]])) / (max(y[, comp[2]]) - min(y, comp[2]))
... ...
@@ -63,6 +109,7 @@ poplin_biplot.default <- function(x, y, comp = 1:2, group,
63 109
   p
64 110
 }
65 111
 
112
+##' @rdname poplin_biplot
66 113
 ##' @export
67 114
 poplin_biplot.poplin.pca <- function(x, scale = 1, comp = 1:2, group, ...) {
68 115
   if (max(comp) > ncol(x) || length(comp) != 2) {
... ...
@@ -81,6 +128,7 @@ poplin_biplot.poplin.pca <- function(x, scale = 1, comp = 1:2, group, ...) {
81 128
   poplin_biplot.default(X, Y, comp = 1:2, group = group, ...)
82 129
 }
83 130
 
131
+##' @rdname poplin_biplot
84 132
 ##' @export
85 133
 poplin_biplot.poplin.plsda <- function(x, comp = 1:2,
86 134
                                        group = attr(x, "Y.observed"), ...) {
... ...
@@ -90,6 +138,7 @@ poplin_biplot.poplin.plsda <- function(x, comp = 1:2,
90 138
 }
91 139
 
92 140
 
141
+##' @rdname poplin_biplot
93 142
 ##' @export
94 143
 poplin_biplot.poplin <- function(x, poplin_in, comp = 1:2, ...) {
95 144
   if (!(poplin_in %in% poplin_reduced_names(x))) {
... ...
@@ -97,6 +146,13 @@ poplin_biplot.poplin <- function(x, poplin_in, comp = 1:2, ...) {
97 146
          "Input must be one of poplin_reduced_names(x).")
98 147
   }
99 148
   m <- poplin_reduced(x, poplin_in)
149
+  if (is.null(colnames(m))) {
150
+    stop("colnames of 'poplin_reduced(x, poplin_in)' must be non-NULL.")
151
+  }
152
+  if (label && is.null(rownames(m))) {
153
+    stop("rownames of 'poplin_reduced(x, poplin_in)' ",
154
+         "'must be non-NULL if label = TRUE.")
155
+  }
100 156
   poplin_biplot(m, comp = comp, ...)
101 157
   ## if (inherits(m, "poplin.pca")) {
102 158
   ##   poplin_biplot.poplin.pca(m, comp = comp, ...)
... ...
@@ -4,6 +4,7 @@
4 4
 ##'
5 5
 ##' @param x A matrix or \linkS4class{poplin} object.
6 6
 ##' @param poplin_in Name of a data matrix to retrieve.
7
+##' @param group A discrete variable to visualize the grouping structure.
7 8
 ##' @param pre_log2 If \code{TRUE}, feature intensities are log2-transformed
8 9
 ##'   before plotting.
9 10
 ##' @param violin If \code{TRUE}, a violin plot is drawn instead of the boxplot.
... ...
@@ -19,7 +19,7 @@
19 19
 ##' @param row_dend_left Logical controlling whether the row dendrogram is
20 20
 ##'   placed on the left on the plot.
21 21
 ##' @param ... Additional arguments passed to [heatmaply::heatmaply].
22
-##' @return gtable of aligned plots.
22
+##' @return gtable of aligned plots
23 23
 ##' @name poplin_naplot
24 24
 NULL
25 25
 
... ...
@@ -1,3 +1,36 @@
1
+##' Score plot of dimension-reduced data
2
+##'
3
+##' Visualize the data onto a lower-dimensional space using the [poplin_reduce]
4
+##' output.
5
+##'
6
+##' @param x A dimension-reduced data matrix produced by [poplin_reduce] or
7
+##'   \linkS4class{poplin} object containing dimension-reduced data.
8
+##' @param poplin_in Name of a dimension-reduced data matrix to retrieve.
9
+##' @param comp a numeric vector of length 2 indicating two components to plot.
10
+##' @param group A discrete variable to visualize the grouping structure.
11
+##' @param group_col A vector of colors with the same length of unique values in
12
+##'   \code{group}.
13
+##' @param point_size The size of points.
14
+##' @param point_shape_by_group Logical controlling whether each group have
15
+##'   different shapes of data points. Also can be a numeric vector with the
16
+##'   same length of unique values in \code{group} to manually set point shapes.
17
+##' @param label Logical controlling whether score labels are shown instead of
18
+##'   points.
19
+##' @param label_subset A character vector specifying a subset of score labels
20
+##'   to display.
21
+##' @param ellipse Logical controlling whether data ellipses are shown using
22
+##'   [ggplot2::stat_ellipse].
23
+##' @param xlab The title of x-axis of the plot.
24
+##' @param ylab The title of y-axis of the plot.
25
+##' @param ylab The title of y-axis of the plot.
26
+##' @param title The main title of the plot.
27
+##' @param legend Logical controlling whether the plot legend is shown.
28
+##' @param ... Arguments passed to the default method.
29
+##' @return A ggplot object.
30
+##' @seealso [poplin_reduce], [poplin_biplot].
31
+##' @name poplin_scoreplot
32
+NULL
33
+
1 34
 ##' @export
2 35
 ##' @importFrom ggplot2 ggplot aes geom_point geom_text stat_ellipse
3 36
 ##' @importFrom ggplot2 xlab ylab ggtitle theme_bw theme element_blank
... ...
@@ -6,6 +39,7 @@ poplin_scoreplot <- function(x, ...) {
6 39
   UseMethod("poplin_scoreplot")
7 40
 }
8 41
 
42
+##' @rdname poplin_scoreplot
9 43
 ##' @export
10 44
 poplin_scoreplot.default <- function(x, comp = c(1, 2), group,
11 45
                                 group_col = NULL,
... ...
@@ -93,6 +127,7 @@ poplin_scoreplot.default <- function(x, comp = c(1, 2), group,
93 127
   }
94 128
 }
95 129
 
130
+##' @rdname poplin_scoreplot
96 131
 ##' @export
97 132
 poplin_scoreplot.poplin.pca <- function(x, comp = c(1, 2),
98 133
                                         group, group_col = NULL,
... ...
@@ -127,6 +162,7 @@ poplin_scoreplot.poplin.pca <- function(x, comp = c(1, 2),
127 162
                            xlab = xlab, ylab = ylab, ...)
128 163
 }
129 164
 
165
+##' @rdname poplin_scoreplot
130 166
 ##' @export
131 167
 poplin_scoreplot.poplin.plsda <- function(x, comp = c(1, 2),
132 168
                                           group = attr(x, "Y.observed"),
... ...
@@ -160,6 +196,7 @@ poplin_scoreplot.poplin.plsda <- function(x, comp = c(1, 2),
160 196
                            xlab = xlab, ylab = ylab, ...)
161 197
 }
162 198
 
199
+##' @rdname poplin_scoreplot
163 200
 ##' @export
164 201
 poplin_scoreplot.poplin <- function(x, poplin_in, comp = 1:2, ...) {
165 202
   if (!(poplin_in %in% poplin_reduced_names(x))) {
... ...
@@ -167,5 +204,12 @@ poplin_scoreplot.poplin <- function(x, poplin_in, comp = 1:2, ...) {
167 204
          "Input must be one of poplin_reduced_names(x).")
168 205
   }
169 206
   m <- poplin_reduced(x, poplin_in)
207
+  if (is.null(colnames(m))) {
208
+    stop("colnames of 'poplin_reduced(x, poplin_in)' must be non-NULL.")
209
+  }
210
+  if (label && is.null(rownames(m))) {
211
+    stop("rownames of 'poplin_reduced(x, poplin_in)' ",
212
+         "'must be non-NULL if label = TRUE.")
213
+  } 
170 214
   poplin_scoreplot(m, comp = comp, ...)
171 215
 }
... ...
@@ -18,7 +18,7 @@
18 18
       stop("'", msg1, "' contains duplicates. ", msg2)
19 19
     }
20 20
     if (any(value_names %in% assay_names)) {
21
-      stop("'", msg1, "' must not overlap with assayNames(x). ", msg2)
21
+      stop("'", msg1, "' must not overlap with poplin_raw_names(x). ", msg2)
22 22
     }
23 23
   }
24 24
 }
... ...
@@ -29,7 +29,7 @@
29 29
   name_pool <- c(assayNames(x), poplin_data_names(x))
30 30
   if (!(name %in% name_pool)) {
31 31
     stop("data '", name, "' is not found in the poplin object.\n",
32
-         "Input must be one of c(assayNames(x), poplin_data_names(x).")
32
+         "Input must be one of c(poplin_raw_names(x), poplin_data_names(x).")
33 33
   }
34 34
   tryCatch(
35 35
     assay(x, name),
... ...
@@ -20,6 +20,8 @@
20 20
 \arguments{
21 21
 \item{x}{A matrix or \linkS4class{poplin} object.}
22 22
 
23
+\item{group}{A discrete variable to visualize the grouping structure.}
24
+
23 25
 \item{pre_log2}{If \code{TRUE}, feature intensities are log2-transformed
24 26
 before plotting.}
25 27
 
... ...
@@ -64,7 +64,7 @@ placed on the left on the plot.}
64 64
 \item{poplin_in}{Name of a data matrix to retrieve.}
65 65
 }
66 66
 \value{
67
-gtable of aligned plots.
67
+gtable of aligned plots
68 68
 }
69 69
 \description{
70 70
 Visualize data for exploring missing value (NA) patterns. All values in a
71 71
new file mode 100644
... ...
@@ -0,0 +1,93 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/plot-scoreplot.R
3
+\name{poplin_scoreplot}
4
+\alias{poplin_scoreplot}
5
+\alias{poplin_scoreplot.default}
6
+\alias{poplin_scoreplot.poplin.pca}
7
+\alias{poplin_scoreplot.poplin.plsda}
8
+\alias{poplin_scoreplot.poplin}
9
+\title{Score plot of dimension-reduced data}
10
+\usage{
11
+\method{poplin_scoreplot}{default}(
12
+  x,
13
+  comp = c(1, 2),
14
+  group,
15
+  group_col = NULL,
16
+  point_size = 1.5,
17
+  point_shape_by_group = FALSE,
18
+  label = FALSE,
19
+  label_size = 3.88,
20
+  label_subset = NULL,
21
+  ellipse = FALSE,
22
+  xlab = NULL,
23
+  ylab = NULL,
24
+  title = NULL,
25
+  legend = TRUE
26
+)
27
+
28
+\method{poplin_scoreplot}{poplin.pca}(
29
+  x,
30
+  comp = c(1, 2),
31
+  group,
32
+  group_col = NULL,
33
+  xlab = NULL,
34
+  ylab = NULL,
35
+  ...
36
+)
37
+
38
+\method{poplin_scoreplot}{poplin.plsda}(
39
+  x,
40
+  comp = c(1, 2),
41
+  group = attr(x, "Y.observed"),
42
+  group_col = NULL,
43
+  xlab = NULL,
44
+  ylab = NULL,
45
+  ...
46
+)
47
+
48
+\method{poplin_scoreplot}{poplin}(x, poplin_in, comp = 1:2, ...)
49
+}
50
+\arguments{
51
+\item{x}{A dimension-reduced data matrix produced by \link{poplin_reduce} or
52
+\linkS4class{poplin} object.}
53
+
54
+\item{comp}{a numeric vector of length 2 indicating two components to plot.}
55
+
56
+\item{group}{A discrete variable to visualize the grouping structure.}
57
+
58
+\item{group_col}{A vector of colors with the same length of unique values in
59
+\code{group}.}
60
+
61
+\item{point_size}{The size of points.}
62
+
63
+\item{point_shape_by_group}{Logical controlling whether each group have
64
+different shapes of data points. Also can be a numeric vector with the
65
+same length of unique values in \code{group} to manually set point shapes.}
66
+
67
+\item{label}{Logical controlling whether text labels are shown instead of
68
+points.}
69
+
70
+\item{label_subset}{A character vector to specify only a subset of text
71
+labels to display.}
72
+
73
+\item{ellipse}{Logical controlling whether data ellipses are shown using
74
+\link[ggplot2:stat_ellipse]{ggplot2::stat_ellipse}.}
75
+
76
+\item{xlab}{The title of x-axis of the plot.}
77
+
78
+\item{ylab}{The title of y-axis of the plot.}
79
+
80
+\item{title}{The main title of the plot.}
81
+
82
+\item{legend}{Logical controlling whether the plot legend is shown.}
83
+
84
+\item{...}{Arguments passed to the default method.}
85
+
86
+\item{poplin_in}{Name of a dimension-reduced data matrix to retrieve.}
87
+}
88
+\value{
89
+A ggplot object.
90
+}
91
+\description{
92
+Visualize the data onto a lower-dimensional space.
93
+}