Browse code

Lint fix

shians authored on 05/04/2024 04:52:02
Showing 1 changed files
... ...
@@ -84,7 +84,7 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
84 84
             }
85 85
             p <- p +
86 86
                 ggplot2::geom_point(aes(colour = .data$group)) +
87
-                ggplot2::scale_color_continuous(name = legend_name) 
87
+                ggplot2::scale_color_continuous(name = legend_name)
88 88
         } else {
89 89
             # discrete colour palette
90 90
             if (is.null(labels)) {
Browse code

Fixed namespace and tests

Shians authored on 17/07/2023 04:13:12
Showing 1 changed files
... ...
@@ -90,7 +90,7 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
90 90
             if (is.null(labels)) {
91 91
                 # no labels, but groups
92 92
                 p <- p + ggplot2::geom_point(aes(colour = .data$group)) +
93
-                    guides(color = guide_legend(title = legend_name))
93
+                    ggplot2::guides(color = ggplot2::guide_legend(title = legend_name))
94 94
             } else {
95 95
                 # labels and groups
96 96
                 # key_glyph causes the legend to display points
Browse code

Updated plot_pca to match plot_mds

Shians authored on 17/07/2023 03:31:56
Showing 1 changed files
... ...
@@ -79,7 +79,9 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
79 79
     } else {
80 80
         if (is.numeric(groups)) {
81 81
             # continuous colour palette ignores labels
82
-            message("Ignoring labels as groups is numeric")
82
+            if (!is.null(labels)) {
83
+                message("Ignoring labels as groups is numeric. Set `labels=NULL` to suppress this message.")
84
+            }
83 85
             p <- p +
84 86
                 ggplot2::geom_point(aes(colour = .data$group)) +
85 87
                 ggplot2::scale_color_continuous(name = legend_name) 
Browse code

Added numeric group handling to mds plot and argument to set legend title

Shians authored on 17/07/2023 02:01:04
Showing 1 changed files
... ...
@@ -10,7 +10,10 @@
10 10
 #'   column names of x, set to NULL to plot points.
11 11
 #' @param groups the character vector of groups the data points will be coloured
12 12
 #'   by. Colour palette can be adjusted using scale_colour_*() functions from
13
-#'   ggplot2.
13
+#'   ggplot2. If groups is numeric, the points will be coloured by a continuous
14
+#'   colour palette. By default, groups is NULL and the points will not be
15
+#'   coloured.
16
+#' @param legend_name the name for the legend.
14 17
 #'
15 18
 #' @return ggplot object of the MDS plot.
16 19
 #'
... ...
@@ -23,7 +26,7 @@
23 26
 #' @importFrom limma plotMDS
24 27
 #' @importFrom ggplot2 draw_key_point
25 28
 #' @export
26
-plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), groups = NULL) {
29
+plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), groups = NULL, legend_name = "group") {
27 30
     if (!is.null(labels)) {
28 31
         assertthat::assert_that(ncol(x) == length(labels))
29 32
     }
... ...
@@ -58,19 +61,43 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
58 61
     }
59 62
 
60 63
     plot_data$labels <- labels
61
-    plot_data$groups <- groups
64
+    plot_data$group <- groups
62 65
 
66
+    p <- ggplot2::ggplot(
67
+        plot_data,
68
+        ggplot2::aes(x = .data$dim1, y = .data$dim2)
69
+    )
63 70
 
64
-    if (!is.null(groups)) {
65
-        p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2, col = .data$groups))
66
-    } else {
67
-        p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2))
68
-    }
69
-
70
-    if (!is.null(labels)) {
71
-        p <- p + ggplot2::geom_label(aes(label = labels), key_glyph = ggplot2::draw_key_point)
71
+    if (is.null(groups)) {
72
+        if (is.null(labels)) {
73
+            # no labels or groups
74
+            p <- p + ggplot2::geom_point()
75
+        } else {
76
+            # no groups, but labels
77
+            p <- p + ggplot2::geom_label(aes(label = labels))
78
+        }
72 79
     } else {
73
-        p <- p + ggplot2::geom_point()
80
+        if (is.numeric(groups)) {
81
+            # continuous colour palette ignores labels
82
+            message("Ignoring labels as groups is numeric")
83
+            p <- p +
84
+                ggplot2::geom_point(aes(colour = .data$group)) +
85
+                ggplot2::scale_color_continuous(name = legend_name) 
86
+        } else {
87
+            # discrete colour palette
88
+            if (is.null(labels)) {
89
+                # no labels, but groups
90
+                p <- p + ggplot2::geom_point(aes(colour = .data$group)) +
91
+                    guides(color = guide_legend(title = legend_name))
92
+            } else {
93
+                # labels and groups
94
+                # key_glyph causes the legend to display points
95
+                p <- p + ggplot2::geom_label(
96
+                    aes(label = labels, colour = .data$group),
97
+                    key_glyph = ggplot2::draw_key_point
98
+                )
99
+            }
100
+        }
74 101
     }
75 102
 
76 103
     p +
Browse code

Fixed tidy semantics broken in fd4f191

Shians authored on 17/01/2023 02:28:09
Showing 1 changed files
... ...
@@ -62,7 +62,7 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
62 62
 
63 63
 
64 64
     if (!is.null(groups)) {
65
-        p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2, col = "groups"))
65
+        p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2, col = .data$groups))
66 66
     } else {
67 67
         p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2))
68 68
     }
Browse code

Fixed deprecation compliance with tidyselect 1.2.0, ggplot 3.4.0

Shians authored on 25/11/2022 05:37:32
Showing 1 changed files
... ...
@@ -62,9 +62,9 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
62 62
 
63 63
 
64 64
     if (!is.null(groups)) {
65
-        p <- ggplot2::ggplot(plot_data, ggplot2::aes_string(x = "dim1", y = "dim2", col = "groups"))
65
+        p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2, col = "groups"))
66 66
     } else {
67
-        p <- ggplot2::ggplot(plot_data, ggplot2::aes_string(x = "dim1", y = "dim2"))
67
+        p <- ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$dim1, y = .data$dim2))
68 68
     }
69 69
 
70 70
     if (!is.null(labels)) {
Browse code

Added namespacing

Shians authored on 10/10/2022 06:34:45
Showing 1 changed files
... ...
@@ -21,6 +21,7 @@
21 21
 #' plot_mds(lmr)
22 22
 #'
23 23
 #' @importFrom limma plotMDS
24
+#' @importFrom ggplot2 draw_key_point
24 25
 #' @export
25 26
 plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), groups = NULL) {
26 27
     if (!is.null(labels)) {
... ...
@@ -67,7 +68,7 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
67 68
     }
68 69
 
69 70
     if (!is.null(labels)) {
70
-        p <- p + ggplot2::geom_label(aes(label = labels), key_glyph = draw_key_point)
71
+        p <- p + ggplot2::geom_label(aes(label = labels), key_glyph = ggplot2::draw_key_point)
71 72
     } else {
72 73
         p <- p + ggplot2::geom_point()
73 74
     }
Browse code

Updated documentation

Shians authored on 07/09/2022 07:21:40
Showing 1 changed files
... ...
@@ -6,9 +6,11 @@
6 6
 #' @param x the log-methylation-ratio matrix.
7 7
 #' @param top the number of top genes used to calculate pairwise distances.
8 8
 #' @param plot_dims the numeric vector of the two dimensions to be plotted.
9
-#' @param labels the character vector of labels for data points. By default uses column names of x, set to NULL to plot
10
-#'   points.
11
-#' @param groups the character vector of groups the data points will be coloured by.
9
+#' @param labels the character vector of labels for data points. By default uses
10
+#'   column names of x, set to NULL to plot points.
11
+#' @param groups the character vector of groups the data points will be coloured
12
+#'   by. Colour palette can be adjusted using scale_colour_*() functions from
13
+#'   ggplot2.
12 14
 #'
13 15
 #' @return ggplot object of the MDS plot.
14 16
 #'
Browse code

Changed legend key for mds text to be point

Shians authored on 07/09/2022 07:13:33
Showing 1 changed files
... ...
@@ -65,10 +65,9 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
65 65
     }
66 66
 
67 67
     if (!is.null(labels)) {
68
-        p <- p + ggplot2::geom_label(aes(label = labels))
68
+        p <- p + ggplot2::geom_label(aes(label = labels), key_glyph = draw_key_point)
69 69
     } else {
70 70
         p <- p + ggplot2::geom_point()
71
-
72 71
     }
73 72
 
74 73
     p +
Browse code

Changed default theme for plot_mds to be consistent with other plots

Shians authored on 17/08/2021 06:55:32
Showing 1 changed files
... ...
@@ -72,7 +72,7 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
72 72
     }
73 73
 
74 74
     p +
75
-        ggplot2::theme_minimal() +
75
+        ggplot2::theme_bw() +
76 76
         ggplot2::xlab(xlabel) +
77 77
         ggplot2::ylab(ylabel) +
78 78
         ggplot2::scale_x_continuous(expand = c(.1, .1))
Browse code

Implemented binary thresholding for plotting functions

Shians authored on 13/08/2021 06:50:42
Showing 1 changed files
... ...
@@ -38,16 +38,25 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
38 38
     var_exp1 <- round(100 * mds_res$var.explained[plot_dims[1]])
39 39
     var_exp2 <- round(100 * mds_res$var.explained[plot_dims[2]])
40 40
 
41
-    plot_data <- data.frame(
42
-        dim1 = mds_res$eigen.vectors[, plot_dims[1]],
43
-        dim2 = mds_res$eigen.vectors[, plot_dims[2]]
44
-    )
41
+    if ("eigen.vectors" %in% names(mds_res)) {
42
+        plot_data <- data.frame(
43
+            dim1 = mds_res$eigen.vectors[, plot_dims[1]],
44
+            dim2 = mds_res$eigen.vectors[, plot_dims[2]]
45
+        )
46
+        xlabel <- glue::glue("Leading logFC Dim {plot_dims[1]} ({var_exp1}%)")
47
+        ylabel <- glue::glue("Leading logFC Dim {plot_dims[2]} ({var_exp2}%)")
48
+    } else {
49
+        plot_data <- data.frame(
50
+            dim1 = mds_res$cmdscale.out[, plot_dims[1]],
51
+            dim2 = mds_res$cmdscale.out[, plot_dims[2]]
52
+        )
53
+        xlabel <- glue::glue("Leading logFC Dim {plot_dims[1]}")
54
+        ylabel <- glue::glue("Leading logFC Dim {plot_dims[2]}")
55
+    }
45 56
 
46 57
     plot_data$labels <- labels
47 58
     plot_data$groups <- groups
48 59
 
49
-    xlabel <- glue::glue("Leading logFC Dim {plot_dims[1]} ({var_exp1}%)")
50
-    ylabel <- glue::glue("Leading logFC Dim {plot_dims[2]} ({var_exp2}%)")
51 60
 
52 61
     if (!is.null(groups)) {
53 62
         p <- ggplot2::ggplot(plot_data, ggplot2::aes_string(x = "dim1", y = "dim2", col = "groups"))
Browse code

Fixed namespacing

Shian Su authored on 27/07/2021 01:44:21
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 #' Plot MDS
2 2
 #'
3 3
 #' Plot multi-dimensional scaling plot using algorithm of limma::plotMDS(). It is recommended this be done with the
4
-#' log-methylation-ratio matrix.
4
+#' log-methylation-ratio matrix generated by bsseq_to_log_methy_ratio().
5 5
 #'
6 6
 #' @param x the log-methylation-ratio matrix.
7 7
 #' @param top the number of top genes used to calculate pairwise distances.
... ...
@@ -30,7 +30,7 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
30 30
     }
31 31
 
32 32
     mds_res <- limma::plotMDS(
33
-        lmr,
33
+        x,
34 34
         top = top,
35 35
         plot = FALSE
36 36
     )
... ...
@@ -50,21 +50,21 @@ plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), gr
50 50
     ylabel <- glue::glue("Leading logFC Dim {plot_dims[2]} ({var_exp2}%)")
51 51
 
52 52
     if (!is.null(groups)) {
53
-        p <- ggplot2::ggplot(plot_data, aes(x = dim1, y = dim2, col = groups))
53
+        p <- ggplot2::ggplot(plot_data, ggplot2::aes_string(x = "dim1", y = "dim2", col = "groups"))
54 54
     } else {
55
-        p <- ggplot2::ggplot(plot_data, aes(x = dim1, y = dim2))
55
+        p <- ggplot2::ggplot(plot_data, ggplot2::aes_string(x = "dim1", y = "dim2"))
56 56
     }
57 57
 
58 58
     if (!is.null(labels)) {
59
-        p <- p + geom_label(aes(label = labels))
59
+        p <- p + ggplot2::geom_label(aes(label = labels))
60 60
     } else {
61
-        p <- p + geom_point()
61
+        p <- p + ggplot2::geom_point()
62 62
 
63 63
     }
64 64
 
65 65
     p +
66
-        theme_minimal() +
67
-        xlab(xlabel) +
68
-        ylab(ylabel) +
69
-        scale_x_continuous(expand = c(.1, .1))
66
+        ggplot2::theme_minimal() +
67
+        ggplot2::xlab(xlabel) +
68
+        ggplot2::ylab(ylabel) +
69
+        ggplot2::scale_x_continuous(expand = c(.1, .1))
70 70
 }
Browse code

Added MDS Plot

Shian Su authored on 26/07/2021 07:22:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,70 @@
1
+#' Plot MDS
2
+#'
3
+#' Plot multi-dimensional scaling plot using algorithm of limma::plotMDS(). It is recommended this be done with the
4
+#' log-methylation-ratio matrix.
5
+#'
6
+#' @param x the log-methylation-ratio matrix.
7
+#' @param top the number of top genes used to calculate pairwise distances.
8
+#' @param plot_dims the numeric vector of the two dimensions to be plotted.
9
+#' @param labels the character vector of labels for data points. By default uses column names of x, set to NULL to plot
10
+#'   points.
11
+#' @param groups the character vector of groups the data points will be coloured by.
12
+#'
13
+#' @return ggplot object of the MDS plot.
14
+#'
15
+#' @examples
16
+#' nmr <- load_example_nanomethresult()
17
+#' bss <- methy_to_bsseq(nmr)
18
+#' lmr <- bsseq_to_log_methy_ratio(bss)
19
+#' plot_mds(lmr)
20
+#'
21
+#' @importFrom limma plotMDS
22
+#' @export
23
+plot_mds <- function(x, top = 500, plot_dims = c(1, 2), labels = colnames(x), groups = NULL) {
24
+    if (!is.null(labels)) {
25
+        assertthat::assert_that(ncol(x) == length(labels))
26
+    }
27
+
28
+    if (!is.null(groups)) {
29
+        assertthat::assert_that(ncol(x) == length(groups))
30
+    }
31
+
32
+    mds_res <- limma::plotMDS(
33
+        lmr,
34
+        top = top,
35
+        plot = FALSE
36
+    )
37
+
38
+    var_exp1 <- round(100 * mds_res$var.explained[plot_dims[1]])
39
+    var_exp2 <- round(100 * mds_res$var.explained[plot_dims[2]])
40
+
41
+    plot_data <- data.frame(
42
+        dim1 = mds_res$eigen.vectors[, plot_dims[1]],
43
+        dim2 = mds_res$eigen.vectors[, plot_dims[2]]
44
+    )
45
+
46
+    plot_data$labels <- labels
47
+    plot_data$groups <- groups
48
+
49
+    xlabel <- glue::glue("Leading logFC Dim {plot_dims[1]} ({var_exp1}%)")
50
+    ylabel <- glue::glue("Leading logFC Dim {plot_dims[2]} ({var_exp2}%)")
51
+
52
+    if (!is.null(groups)) {
53
+        p <- ggplot2::ggplot(plot_data, aes(x = dim1, y = dim2, col = groups))
54
+    } else {
55
+        p <- ggplot2::ggplot(plot_data, aes(x = dim1, y = dim2))
56
+    }
57
+
58
+    if (!is.null(labels)) {
59
+        p <- p + geom_label(aes(label = labels))
60
+    } else {
61
+        p <- p + geom_point()
62
+
63
+    }
64
+
65
+    p +
66
+        theme_minimal() +
67
+        xlab(xlabel) +
68
+        ylab(ylabel) +
69
+        scale_x_continuous(expand = c(.1, .1))
70
+}