Skip to content

Commit c05d218

Browse files
Merge pull request #42 from zhanghao-njmu/develop
Develop
2 parents e409868 + 10ae0b5 commit c05d218

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

R/SCP-plot.R

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6186,7 +6186,6 @@ ExpCorPlot <- function(srt, features, group.by = NULL, split.by = NULL, cells =
61866186
}
61876187
}
61886188

6189-
61906189
#' CellDensityPlot
61916190
#'
61926191
#' @examples
@@ -6201,7 +6200,8 @@ ExpCorPlot <- function(srt, features, group.by = NULL, split.by = NULL, cells =
62016200
#' @importFrom ggplot2 ggplot scale_fill_manual labs scale_y_discrete scale_x_continuous facet_grid labs coord_flip element_text element_line
62026201
#' @importFrom cowplot plot_grid
62036202
#' @export
6204-
CellDensityPlot <- function(srt, features, group.by, split.by = NULL, flip = FALSE, reverse = FALSE,
6203+
CellDensityPlot <- function(srt, features, group.by, split.by = NULL,
6204+
flip = FALSE, reverse = FALSE, x_order = c("value", "rank"),
62056205
decreasing = NULL, palette = "Paired", palcolor = NULL,
62066206
cells = NULL, assay = NULL, slot = "data", keep_empty = FALSE,
62076207
y.nbreaks = 4, y.min = NULL, y.max = NULL, same.y.lims = FALSE,
@@ -6210,6 +6210,7 @@ CellDensityPlot <- function(srt, features, group.by, split.by = NULL, flip = FAL
62106210
combine = TRUE, nrow = NULL, ncol = NULL, byrow = TRUE, align = "hv", axis = "lr", force = FALSE) {
62116211
check_R("ggridges")
62126212
assay <- assay %||% DefaultAssay(srt)
6213+
x_order <- match.arg(x_order)
62136214
if (is.null(features)) {
62146215
stop("'features' must be provided.")
62156216
}
@@ -6287,7 +6288,11 @@ CellDensityPlot <- function(srt, features, group.by, split.by = NULL, flip = FAL
62876288
dat[, f][dat[, f] == min(dat[, f])] <- min(dat[, f][is.finite(dat[, f])])
62886289
}
62896290
dat[, "cell"] <- rownames(dat)
6290-
dat[, "value"] <- dat[, f]
6291+
if (x_order == "value") {
6292+
dat[, "value"] <- dat[, f]
6293+
} else {
6294+
dat[, "value"] <- rank(dat[, f])
6295+
}
62916296
dat[, "features"] <- f
62926297
dat[, "split.by"] <- s
62936298
dat <- dat[!is.na(dat[[f]]), ]
@@ -6303,10 +6308,10 @@ CellDensityPlot <- function(srt, features, group.by, split.by = NULL, flip = FAL
63036308
levels <- dat %>%
63046309
group_by_at(g) %>%
63056310
summarise_at(.funs = median, .vars = f, na.rm = TRUE) %>%
6306-
arrange_at(.vars = f, .funs = if (decreasing) desc else list(), na.rm = TRUE) %>%
6311+
arrange_at(.vars = f, .funs = if (decreasing) desc else list()) %>%
63076312
pull(g) %>%
63086313
as.character()
6309-
dat[["order"]] <- factor(dat[[g]], levels = rev(levels))
6314+
dat[["order"]] <- factor(dat[[g]], levels = levels)
63106315
} else {
63116316
dat[["order"]] <- factor(dat[[g]], levels = rev(levels(dat[[g]])))
63126317
}
@@ -6317,7 +6322,7 @@ CellDensityPlot <- function(srt, features, group.by, split.by = NULL, flip = FAL
63176322
aspect.ratio <- NULL
63186323
}
63196324
}
6320-
p <- ggplot(dat, aes(x = .data[[f]], y = .data[["order"]], fill = .data[[g]])) +
6325+
p <- ggplot(dat, aes(x = .data[["value"]], y = .data[["order"]], fill = .data[[g]])) +
63216326
ggridges::geom_density_ridges()
63226327
p <- p + scale_fill_manual(
63236328
name = paste0(g, ":"),
@@ -9248,15 +9253,16 @@ SummaryPlot <- function(srt,
92489253
#' @importFrom grDevices colorRampPalette
92499254
#' @importFrom stats runif
92509255
#' @export
9251-
DynamicPlot <- function(srt, features, lineages, cells = NULL, slot = "counts", assay = "RNA", family = NULL,
9256+
DynamicPlot <- function(srt, features, lineages, group.by = NULL, cells = NULL, slot = "counts", assay = "RNA", family = NULL,
92529257
exp_method = c("log1p", "raw", "zscore", "fc", "log2fc"), lib_normalize = TRUE, libsize = NULL,
9253-
order.by = "pseudotime", group.by = NULL, compare_lineages = TRUE, compare_features = FALSE,
9258+
compare_lineages = TRUE, compare_features = FALSE,
92549259
add_line = TRUE, add_interval = TRUE, line.size = 1, line_palette = "Dark2", line_palcolor = NULL,
92559260
add_point = TRUE, pt.size = 1, point_palette = "Paired", point_palcolor = NULL,
9256-
add_rug = TRUE, flip = FALSE, reverse = FALSE,
9261+
add_rug = TRUE, flip = FALSE, reverse = FALSE, x_order = c("value", "rank"),
92579262
aspect.ratio = NULL, legend.position = "right", legend.direction = "vertical",
92589263
combine = TRUE, nrow = NULL, ncol = NULL, byrow = TRUE, align = "hv", axis = "lr") {
92599264
check_R("MatrixGenerics")
9265+
x_order <- match.arg(x_order)
92609266
if (!is.null(group.by) && !group.by %in% colnames(srt@meta.data)) {
92619267
stop(group.by, " is not in the meta.data of srt object.")
92629268
}
@@ -9314,9 +9320,6 @@ DynamicPlot <- function(srt, features, lineages, cells = NULL, slot = "counts",
93149320
}
93159321

93169322
x_assign <- rowMeans(srt@meta.data[cell_union, lineages, drop = FALSE], na.rm = TRUE)
9317-
if (order.by == "rank") {
9318-
x_assign <- rank(x_assign)
9319-
}
93209323
cell_metadata <- cbind.data.frame(data.frame(row.names = cell_union),
93219324
x_assign = x_assign,
93229325
srt@meta.data[cell_union, lineages, drop = FALSE]
@@ -9458,8 +9461,10 @@ DynamicPlot <- function(srt, features, lineages, cells = NULL, slot = "counts",
94589461
for (l in lineages_use) {
94599462
for (f in features_use) {
94609463
df <- subset(df_all, df_all[["Lineages"]] %in% l & df_all[["Features"]] %in% f)
9461-
random_noise <- runif(nrow(df), -0.01 * diff(range(df[, "exp", drop = FALSE], na.rm = TRUE)), 0.01 * diff(range(df[, "exp", drop = FALSE], na.rm = TRUE)))
9462-
df[, "random_noise"] <- random_noise
9464+
if (x_order == "rank") {
9465+
df[, "x_assign"] <- rank(df[, "x_assign"])
9466+
df[, "Pseudotime"] <- rank(df[, "Pseudotime"])
9467+
}
94639468
df_point <- unique(df[df[["Value"]] == "raw", c("Cell", "x_assign", "exp", group.by)])
94649469
if (isTRUE(compare_features)) {
94659470
raw_point <- NULL
@@ -9560,7 +9565,7 @@ DynamicPlot <- function(srt, features, lineages, cells = NULL, slot = "counts",
95609565
rug +
95619566
interval +
95629567
line +
9563-
labs(x = "Pseudotime", y = exp_name) +
9568+
labs(x = ifelse(x_order == "rank", "Pseudotime(rank)", "Pseudotime"), y = exp_name) +
95649569
facet_grid(formula(formula), scales = "free") +
95659570
theme_scp(
95669571
aspect.ratio = aspect.ratio,
@@ -10150,6 +10155,7 @@ DynamicHeatmap <- function(srt, lineages, features = NULL, feature_from = lineag
1015010155
lineage_cells <- gsub(pattern = l, replacement = "", x = cell_order_list[[l]])
1015110156
subplots <- CellDensityPlot(
1015210157
srt = srt, cells = lineage_cells, group.by = cellan, features = l,
10158+
decreasing = TRUE, x_order = "rank",
1015310159
palette = palette, palcolor = palcolor,
1015410160
flip = flip, reverse = l %in% lineages[reverse_ht] || l %in% reverse_ht
1015510161
) + theme_void()
@@ -10195,7 +10201,7 @@ DynamicHeatmap <- function(srt, lineages, features = NULL, feature_from = lineag
1019510201
subplots <- DynamicPlot(
1019610202
srt = srt, cells = lineage_cells, lineages = l, group.by = NULL, features = cellan,
1019710203
line_palette = palette, line_palcolor = palcolor,
10198-
add_rug = FALSE, legend.position = "none", compare_features = TRUE,
10204+
add_rug = FALSE, legend.position = "none", compare_features = TRUE, x_order = "rank",
1019910205
flip = flip, reverse = l %in% lineages[reverse_ht] || l %in% reverse_ht
1020010206
) + theme_void()
1020110207
subplots_list[[paste0(paste0(cellan, collapse = ","), ":", l)]] <- subplots

README/README-DynamicHeatmap-1.png

13 KB
Loading

README/README-DynamicPlot-1.png

-308 Bytes
Loading

man/CellDensityPlot.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/DynamicPlot.Rd

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

0 commit comments

Comments
 (0)