Skip to content

Commit 720df74

Browse files
committed
Update to plot highlighted points last
1 parent eb75b95 commit 720df74

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

R/plot_dist.R

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
plot_dist <- function(d,
6767
method = c("cmds", "isomds", "tsne"),
6868
highlight = NULL,
69-
gp = NULL, point.alpha = 0.6) {
69+
gp = NULL, point.alpha = 0.8) {
7070

7171
# Checks ----
7272
method <- match.arg(method)
7373

74+
d_labs <- attr(d, "Labels")
75+
7476
if (!is.null(gp)) {
75-
missing <- setdiff(labels(d), names(gp))
77+
missing <- setdiff(d_labs, names(gp))
7678
if (length(missing) > 0) {
7779
warning('Some objects in distance matrix "d" have no group assignment: ',
7880
paste(missing, collapse = ", "))
@@ -83,7 +85,7 @@ plot_dist <- function(d,
8385
if (!inherits(d, "dist")) {
8486
stop('"d" should be a distance matrix of class "dist".')
8587
}
86-
dist_labels <- labels(d) # No dups as it is a distance matrix
88+
dist_labels <- d_labs # No dups as it is a distance matrix
8789

8890
if (is.null(dist_labels)) {
8991
stop('Labels are missing in distance matrix "d".')
@@ -96,8 +98,8 @@ plot_dist <- function(d,
9698
}
9799

98100
# check if highlight is present in the entire set
99-
if (any(!(highlight %in% labels(d)))) {
100-
alsel_miss <- highlight[!(highlight %in% labels(d))]
101+
if (any(!(highlight %in% d_labs))) {
102+
alsel_miss <- highlight[!(highlight %in% d_labs)]
101103
stop(paste('The following entry/entries specified in "highlight" ',
102104
'are not present in "data":\n',
103105
paste(alsel_miss, collapse = ", "),
@@ -110,35 +112,40 @@ plot_dist <- function(d,
110112
stop('"point.alpha" should be a numeric vector of unit length.')
111113
}
112114

113-
n <- nrow(as.matrix(d))
115+
n <- attr(d, "Size")
114116

115117
# fallback for small matrices
116118
if (method == "isomds" && n < 3) {
117119
message("isoMDS requires n >= 3; falling back to cmds.")
118120
method <- "cmds"
119121
}
120122

123+
if (method == "tsne" && n < 4) {
124+
message("tsne requires n >= 4; falling back to cmds.")
125+
method <- "cmds"
126+
}
127+
121128
# Dimensional reduction ----
122129
if (method == "cmds") {
123130
fit <- cmdscale(d, k = 2)
124131
}
125132

126133
if (method == "isomds") {
127-
fit <- MASS::isoMDS(as.dist(d), trace = F)$points
134+
fit <- MASS::isoMDS(d, trace = FALSE)$points
128135
}
129136

130137
if (method == "tsne") {
131138

132-
prplx <- min(30, floor((n - 1) / 3))
139+
prplx <- max(1, min(30, floor((n - 1) / 3)))
133140

134141
fit <- Rtsne::Rtsne(as.matrix(d),
135142
is_distance = TRUE, perplexity = prplx)$Y
136143

137-
rownames(fit) <- labels(d)
144+
rownames(fit) <- d_labs
138145
}
139146

140147
# Labels ----
141-
labs <- labels(d)
148+
labs <- d_labs
142149

143150
# Plotting dataframe ----
144151
df <- data.frame(sample = labs,
@@ -153,6 +160,12 @@ plot_dist <- function(d,
153160
"other"
154161
}
155162

163+
# Ensure highlighted points are plotted last
164+
df$highlight <- factor(df$highlight,
165+
levels = c("other", "highlight"))
166+
167+
df <- df[order(df$highlight), ]
168+
156169
# Groups ----
157170
if (!is.null(gp)) {
158171

man/plot_dist.Rd

Lines changed: 1 addition & 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)