Skip to content

Commit f2d53e7

Browse files
committed
update visualize.score(), visualize.loading(), visualize.biplot(): add argument axes to PCA plots for selecting different PCs
1 parent bd26700 commit f2d53e7

6 files changed

Lines changed: 95 additions & 38 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ License: GPL (>= 3)
2424
Encoding: UTF-8
2525
Roxygen: list(roclets = c("collate", "namespace", "rd",
2626
"roxyglobals::global_roclet"))
27-
RoxygenNote: 7.3.3
2827
Depends: R (>= 4.1.0)
2928
Imports:
3029
RColorBrewer,
@@ -68,3 +67,4 @@ Config/roxyglobals/unique: FALSE
6867
URL: https://github.com/uconn-scs/msDiaLogue
6968
BugReports: https://github.com/uconn-scs/msDiaLogue/issues
7069
LazyData: true
70+
Config/roxygen2/version: 8.0.0

R/globals.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ utils::globalVariables(c(
5858
"p-value", # <visualize.volcano>
5959
"Label", # <visualize.volcano>
6060
"percent", # <visualize.scree>
61-
"Dim.1", # <visualize.loading>
62-
"Dim.2", # <visualize.loading>
61+
"x", # <visualize.loading>
62+
"y", # <visualize.loading>
6363
"Name", # <visualize.loading>
6464
"xcircle", # <visualize.loading>
6565
"ycircle", # <visualize.loading>

R/visualization.R

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ visualize.scree <- function(dataSet, type = c("bar", "line"),
984984
#' @param dataSet The data set corresponds to the output from the function
985985
#' \code{\link[msDiaLogue]{analyze.pca}} or \code{\link[msDiaLogue]{analyze.plsda}}.
986986
#'
987+
#' @param axes A numeric vector (default = c(1, 2)) specifying
988+
#' the axes of interest.
989+
#'
987990
#' @param ellipse A logical value (default = TRUE) specifying whether
988991
#' to draw ellipses around the individuals.
989992
#'
@@ -998,11 +1001,21 @@ visualize.scree <- function(dataSet, type = c("bar", "line"),
9981001
#'
9991002
#' @export
10001003

1001-
visualize.score <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, label = TRUE) {
1004+
visualize.score <- function(dataSet, axes = c(1, 2),
1005+
ellipse = TRUE, ellipse.level = 0.95,
1006+
label = TRUE) {
10021007

10031008
## individual coordinates (scores)
10041009
ind.coord <- dataSet$scores
1005-
colnames(ind.coord) <- paste0("Dim.", 1:ncol(ind.coord))
1010+
ndim <- ncol(ind.coord)
1011+
colnames(ind.coord) <- paste0("Dim.", 1:ndim)
1012+
1013+
if(length(axes) != 2) {
1014+
stop("`axes` must have length 2.")
1015+
}
1016+
if (max(axes) > ndim) {
1017+
stop("`axes` must be between 1 and ", ndim, ".")
1018+
}
10061019

10071020
## percentage of variance explained by each component
10081021
if (inherits(dataSet, "pca")) {
@@ -1015,21 +1028,21 @@ visualize.score <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, label
10151028
## extract coordinates for selected axes
10161029
ind <- data.frame(Group = factor(gsub("_.*", "", rownames(ind.coord))),
10171030
Name = gsub("^.*_", "", rownames(ind.coord)),
1018-
ind.coord[, c(1,2), drop = FALSE],
1031+
setNames(as.data.frame(ind.coord[, axes, drop = FALSE]), c("x", "y")),
10191032
stringsAsFactors = TRUE)
10201033

1021-
ggplot(ind, aes(x = Dim.1, y = Dim.2, color = Group, shape = Group)) +
1034+
ggplot(ind, aes(x = x, y = y, color = Group, shape = Group)) +
10221035
geom_point() +
10231036
{ if (ellipse) stat_ellipse(aes(group = Group, fill = Group),
10241037
geom = "polygon", alpha = 0.1,
10251038
type = "norm", level = ellipse.level,
1026-
linetype = "solid", size = 0.5, show.legend = TRUE) } +
1039+
linetype = "solid", linewidth = 0.5, show.legend = TRUE) } +
10271040
{ if (label) geom_text_repel(aes(label = Name), size = 3,
10281041
max.overlaps = Inf, show.legend = FALSE) } +
10291042
geom_hline(yintercept = 0, color = "black", linetype = "dashed") +
10301043
geom_vline(xintercept = 0, color = "black", linetype = "dashed") +
1031-
labs(x = paste0("Dim.1 (", round(variance[1], 1), "%)"),
1032-
y = paste0("Dim.2 (", round(variance[2], 1), "%)")) +
1044+
labs(x = sprintf("Dim.%d (%.1f%%)", axes[1], variance[axes[1]]),
1045+
y = sprintf("Dim.%d (%.1f%%)", axes[2], variance[axes[2]])) +
10331046
theme_bw() +
10341047
theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5))
10351048

@@ -1045,6 +1058,9 @@ visualize.score <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, label
10451058
#' @param dataSet The data set corresponds to the output from the function
10461059
#' \code{\link[msDiaLogue]{analyze.pca}} or \code{\link[msDiaLogue]{analyze.plsda}}.
10471060
#'
1061+
#' @param axes A numeric vector (default = c(1, 2)) specifying
1062+
#' the axes of interest.
1063+
#'
10481064
#' @param label A logical value (default = TRUE) specifying whether
10491065
#' the active variables to be labeled.
10501066
#'
@@ -1055,7 +1071,7 @@ visualize.score <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, label
10551071
#'
10561072
#' @export
10571073

1058-
visualize.loading <- function(dataSet, label = TRUE) {
1074+
visualize.loading <- function(dataSet, axes = c(1, 2), label = TRUE) {
10591075

10601076
## variable-component correlaiton
10611077
## percentage of variance explained by each component
@@ -1067,22 +1083,30 @@ visualize.loading <- function(dataSet, label = TRUE) {
10671083
var.coord <- sweep(dataSet$loadings, 2, sqrt(dataSet$Xvar/dataSet$Xtotvar), "*")
10681084
variance <- 100 * dataSet$Xvar / dataSet$Xtotvar
10691085
}
1070-
colnames(var.coord) <- paste0("Dim.", 1:ncol(var.coord))
1086+
ndim <- ncol(var.coord)
1087+
colnames(var.coord) <- paste0("Dim.", 1:ndim)
1088+
1089+
if(length(axes) != 2) {
1090+
stop("`axes` must have length 2.")
1091+
}
1092+
if (max(axes) > ndim) {
1093+
stop("`axes` must be between 1 and ", ndim, ".")
1094+
}
10711095

10721096
## combine into result data frame
10731097
var <- data.frame(Name = rownames(var.coord),
1074-
var.coord[, c(1,2), drop = FALSE],
1098+
setNames(as.data.frame(var.coord[, axes, drop = FALSE]), c("x", "y")),
10751099
xstart = 0, ystart = 0,
10761100
stringsAsFactors = TRUE)
10771101

1078-
plot <- ggplot(var, aes(x = Dim.1, y = Dim.2)) +
1079-
geom_segment(data = var, aes(x = 0, y = 0, xend = Dim.1, yend = Dim.2),
1102+
plot <- ggplot(var, aes(x = x, y = y)) +
1103+
geom_segment(data = var, aes(x = 0, y = 0, xend = x, yend = y),
10801104
arrow = arrow(length = unit(0.2, "cm")), color = "black") +
10811105
{ if (label) geom_text_repel(aes(label = Name), size = 3, max.overlaps = Inf) } +
10821106
geom_hline(yintercept = 0, color = "black", linetype = "dashed") +
10831107
geom_vline(xintercept = 0, color = "black", linetype = "dashed") +
1084-
labs(x = paste0("Dim1 (", round(variance[1], 1), "%)"),
1085-
y = paste0("Dim2 (", round(variance[2], 1), "%)")) +
1108+
labs(x = sprintf("Dim.%d (%.1f%%)", axes[1], variance[axes[1]]),
1109+
y = sprintf("Dim.%d (%.1f%%)", axes[2], variance[axes[2]])) +
10861110
theme_bw() +
10871111
theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5))
10881112

@@ -1108,6 +1132,9 @@ visualize.loading <- function(dataSet, label = TRUE) {
11081132
#' @param dataSet The data set corresponds to the output from the function
11091133
#' \code{\link[msDiaLogue]{analyze.pca}} or \code{\link[msDiaLogue]{analyze.plsda}}.
11101134
#'
1135+
#' @param axes A numeric vector (default = c(1, 2)) specifying
1136+
#' the axes of interest.
1137+
#'
11111138
#' @param ellipse A logical value (default = TRUE) specifying whether
11121139
#' to draw ellipses around the individuals.
11131140
#'
@@ -1128,15 +1155,24 @@ visualize.loading <- function(dataSet, label = TRUE) {
11281155
#'
11291156
#' @export
11301157

1131-
visualize.biplot <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, label = "all") {
1132-
1158+
visualize.biplot <- function(dataSet, axes = c(1, 2),
1159+
ellipse = TRUE, ellipse.level = 0.95,
1160+
label = "all") {
11331161
## individual coordinates (scores)
11341162
ind.coord <- dataSet$scores
1135-
colnames(ind.coord) <- paste0("Dim.", 1:ncol(ind.coord))
1163+
ndim <- ncol(ind.coord)
1164+
colnames(ind.coord) <- paste0("Dim.", 1:ndim)
1165+
1166+
if(length(axes) != 2) {
1167+
stop("`axes` must have length 2.")
1168+
}
1169+
if (max(axes) > ndim) {
1170+
stop("`axes` must be between 1 and ", ndim, ".")
1171+
}
11361172

11371173
ind <- data.frame(Group = factor(gsub("_.*", "", rownames(ind.coord))),
11381174
Name = gsub("^.*_", "", rownames(ind.coord)),
1139-
ind.coord[, c(1,2), drop = FALSE],
1175+
setNames(as.data.frame(ind.coord[, axes, drop = FALSE]), c("x", "y")),
11401176
stringsAsFactors = TRUE)
11411177

11421178
## variable-component correlaiton
@@ -1149,36 +1185,36 @@ visualize.biplot <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, labe
11491185
var.coord <- sweep(dataSet$loadings, 2, sqrt(dataSet$Xvar/dataSet$Xtotvar), "*")
11501186
variance <- 100 * dataSet$Xvar / dataSet$Xtotvar
11511187
}
1152-
colnames(var.coord) <- paste0("Dim.", 1:ncol(var.coord))
1188+
colnames(var.coord) <- paste0("Dim.", 1:ndim)
11531189

11541190
var <- data.frame(Name = rownames(var.coord),
1155-
var.coord[, c(1,2), drop = FALSE],
1191+
setNames(as.data.frame(var.coord[, axes, drop = FALSE]), c("x", "y")),
11561192
xstart = 0, ystart = 0,
11571193
stringsAsFactors = TRUE)
11581194

1159-
r <- min(max(ind[, "Dim.1"]) - min(ind[, "Dim.1"])/(max(var[, "Dim.1"]) - min(var[, "Dim.1"])),
1160-
max(ind[, "Dim.2"]) - min(ind[, "Dim.2"])/(max(var[, "Dim.2"]) - min(var[, "Dim.2"])))
1195+
r <- min(max(ind[, "x"]) - min(ind[, "x"])/(max(var[, "x"]) - min(var[, "x"])),
1196+
max(ind[, "y"]) - min(ind[, "y"])/(max(var[, "y"]) - min(var[, "y"])))
11611197

1162-
var[, c("Dim.1", "Dim.2")] <- var[, c("Dim.1", "Dim.2")] * r * 0.7
1198+
var[, c("x", "y")] <- var[, c("x", "y")] * r * 0.7
11631199

1164-
ggplot(ind, aes(x = Dim.1, y = Dim.2, color = Group, shape = Group)) +
1200+
ggplot(ind, aes(x = x, y = y, color = Group, shape = Group)) +
11651201
geom_point() +
11661202
{ if (ellipse) stat_ellipse(aes(group = Group, fill = Group),
11671203
geom = "polygon", alpha = 0.1,
11681204
type = "norm", level = ellipse.level,
1169-
linetype = "solid", size = 0.5, show.legend = TRUE) } +
1205+
linetype = "solid", linewidth = 0.5, show.legend = TRUE) } +
11701206
{ if (label %in% c("all", "ind")) geom_text_repel(aes(label = Name), size = 3,
11711207
max.overlaps = Inf, show.legend = FALSE) } +
11721208
geom_segment(inherit.aes = FALSE, data = var,
1173-
aes(x = 0, y = 0, xend = Dim.1, yend = Dim.2),
1209+
aes(x = 0, y = 0, xend = x, yend = y),
11741210
arrow = arrow(length = unit(0.2, "cm")), color = "black") +
11751211
{ if (label %in% c("all", "var")) geom_text_repel(inherit.aes = FALSE, data = var,
1176-
aes(x = Dim.1, y = Dim.2, label = Name),
1212+
aes(x = x, y = y, label = Name),
11771213
size = 3, max.overlaps = Inf) } +
11781214
geom_hline(yintercept = 0, color = "black", linetype = "dashed") +
11791215
geom_vline(xintercept = 0, color = "black", linetype = "dashed") +
1180-
labs(x = paste0("Dim1 (", round(variance[1], 1), "%)"),
1181-
y = paste0("Dim2 (", round(variance[2], 1), "%)")) +
1216+
labs(x = sprintf("Dim.%d (%.1f%%)", axes[1], variance[axes[1]]),
1217+
y = sprintf("Dim.%d (%.1f%%)", axes[2], variance[axes[2]])) +
11821218
theme_bw() +
11831219
theme(legend.position = "bottom", plot.title = element_text(hjust = 0.5))
11841220

@@ -1219,9 +1255,9 @@ visualize.biplot <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, labe
12191255
visualize.vip <- function(dataSet, comp = 1, num = 10, thres = 1, rel.widths) {
12201256

12211257
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
1222-
scaffoldCheck <- any(colnames(information) == "Visible?")
1223-
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
1224-
labelCol <- ifelse(scaffoldCheck, "AlternateID", "PG.ProteinName")
1258+
scaffoldCheck <- "Visible?" %in% colnames(information)
1259+
IDcol <- if (scaffoldCheck) "AccessionNumber" else "PG.ProteinName"
1260+
labelCol <- if (scaffoldCheck) "AlternateID" else "PG.ProteinName"
12251261

12261262
vips <- as.data.frame(dataSet[["vips"]]) %>%
12271263
select(Score = paste("Comp", comp)) %>%

man/visualize.biplot.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/visualize.loading.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/visualize.score.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)