Skip to content

Commit ee207eb

Browse files
authored
Merge pull request #10198 from satijalab/fix/labelclusters
Add color retrieval logic in LabelClusters for consistent cluster label coloring
2 parents 244d594 + 4a8ef09 commit ee207eb

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

R/visualization.R

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6131,7 +6131,15 @@ LabelClusters <- function(
61316131
data[, xynames["y"]] <- data[, xynames["y"]] + sum(y.transform)
61326132
}
61336133
}
6134-
data <- cbind(data, color = pb$data[[1]][[1]])
6134+
6135+
# Retrieve colour from built data
6136+
col_choice <- intersect(c("colour", "color"), names(pb$data[[1]]))
6137+
if (length(col_choice) > 0) {
6138+
data <- cbind(data, color = pb$data[[1]][[col_choice[1]]])
6139+
} else {
6140+
data <- cbind(data, color = NA_character_)
6141+
}
6142+
61356143
labels.loc <- lapply(
61366144
X = groups,
61376145
FUN = function(group) {
@@ -6146,7 +6154,7 @@ LabelClusters <- function(
61466154
split_data <- data.use[split_by_values == split, , drop = FALSE]
61476155
# Extract coordinates
61486156
if (inherits(split_data, "sf")) {
6149-
st_agr(split_data) <- "constant" # Set attr-geom relationship to avoid warnings
6157+
st_agr(split_data) <- "constant"
61506158
coord_data <- data.frame(sf::st_coordinates(sf::st_centroid(split_data)))
61516159
names(coord_data) <- xynames[1:2]
61526160
} else {
@@ -6167,7 +6175,7 @@ LabelClusters <- function(
61676175
} else {
61686176
# Extract coordinates
61696177
if (inherits(data.use, "sf")) {
6170-
st_agr(data.use) <- "constant" # Set attr-geom relationship to avoid warnings
6178+
st_agr(data.use) <- "constant"
61716179
coord_data <- data.frame(sf::st_coordinates(sf::st_centroid(data.use)))
61726180
names(coord_data) <- xynames[1:2]
61736181
} else {
@@ -6190,7 +6198,7 @@ LabelClusters <- function(
61906198
# Handle sf data subsetting for nearest point calculation
61916199
if (inherits(data, "sf")) {
61926200
group.data <- data[as.character(data[[id]]) == as.character(x[3]), ]
6193-
st_agr(group.data) <- "constant" # Set attr-geom relationship to avoid warnings
6201+
st_agr(group.data) <- "constant"
61946202
group.data <- data.frame(sf::st_coordinates(sf::st_centroid(group.data)))
61956203
names(group.data) <- xynames[1:2]
61966204
} else {
@@ -6214,20 +6222,23 @@ LabelClusters <- function(
62146222
for (group in groups) {
62156223
labels.loc[labels.loc[, id] == group, id] <- labels[group]
62166224
}
6225+
62176226
if (box) {
62186227
geom.use <- ifelse(test = repel, yes = geom_label_repel, no = geom_label)
62196228
plot <- plot + geom.use(
62206229
data = labels.loc,
6221-
mapping = aes(x = .data[[xynames['x']]], y = .data[[xynames['y']]], label = .data[[id]], fill = .data[[id]]),
6230+
mapping = aes(x = .data[[xynames['x']]], y = .data[[xynames['y']]], label = .data[[id]], fill = .data[["color"]]),
62226231
show.legend = FALSE,
6232+
inherit.aes = FALSE,
62236233
...
6224-
)
6234+
) + scale_fill_identity()
62256235
} else {
62266236
geom.use <- ifelse(test = repel, yes = geom_text_repel, no = geom_text)
62276237
plot <- plot + geom.use(
62286238
data = labels.loc,
62296239
mapping = aes(x = .data[[xynames['x']]], y = .data[[xynames['y']]], label = .data[[id]]),
62306240
show.legend = FALSE,
6241+
inherit.aes = FALSE,
62316242
...
62326243
)
62336244
}

0 commit comments

Comments
 (0)