Skip to content

Commit

Permalink
fix: document ...
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanppetkov committed Feb 16, 2025
1 parent 48a6407 commit c3a9281
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 95 deletions.
181 changes: 92 additions & 89 deletions R/density_scatter_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#' get_density(x, y, n = 100)
#'
.get2Ddensity <- function(
x,
y,
n = 500,
...) {
x,
y,
n = 500,
...
) {
# Check that MASS is installed
expect_MASS()

Expand Down Expand Up @@ -65,6 +66,7 @@
#' @param annotation_params Optional list of parameters to pass to geom_text for gate annotations. Common parameters
#' include color (text color), vjust (vertical justification), hjust (horizontal justification), and size (text size).
#' @param colors Optional character vector of colors to use for the color scale.
#' @param ... Additional arguments to pass to `MASS::kde2d`.
#'
#' @return A ggplot object.
#'
Expand Down Expand Up @@ -158,21 +160,22 @@
#' @export
#'
DensityScatterPlot <- function(
object,
marker1,
marker2,
facet_vars = NULL,
plot_gate = NULL,
grid_n = 100,
scale_density = TRUE,
margin_density = TRUE,
pt_size = 1,
alpha = 1,
layer = NULL,
coord_fixed = TRUE,
annotation_params = NULL,
colors = NULL,
...) {
object,
marker1,
marker2,
facet_vars = NULL,
plot_gate = NULL,
grid_n = 500,
scale_density = TRUE,
margin_density = TRUE,
pt_size = 1,
alpha = 1,
layer = NULL,
coord_fixed = TRUE,
annotation_params = NULL,
colors = NULL,
...
) {
# Validate input parameters
assert_vector(facet_vars, type = "character", n = 1, allow_null = TRUE)
assert_max_length(facet_vars, n = 2, allow_null = TRUE)
Expand Down Expand Up @@ -252,35 +255,35 @@ DensityScatterPlot <- function(

plot_data <-
plot_data %>%
rename(
marker1 = !!marker1,
marker2 = !!marker2
) %>%
group_by_at(facet_vars) %>%
mutate(dens = .get2Ddensity(marker1, marker2, n = grid_n, ...)) %>%
ungroup()
rename(
marker1 = !!marker1,
marker2 = !!marker2
) %>%
group_by_at(facet_vars) %>%
mutate(dens = .get2Ddensity(marker1, marker2, n = grid_n, ...)) %>%
ungroup()

if (isTRUE(scale_density)) {
plot_data <-
plot_data %>%
group_by_at(facet_vars) %>%
mutate(dens = dens / max(dens)) %>%
ungroup()
group_by_at(facet_vars) %>%
mutate(dens = dens / max(dens)) %>%
ungroup()
}

# Set plot theme
if (is.null(facet_vars)) {
plot_theme <-
theme_bw() +
theme(
panel.grid = element_blank(),
panel.spacing = unit(0, "lines"),
plot.margin = unit(c(0, 0, 0, 0), "lines")
)
theme(
panel.grid = element_blank(),
panel.spacing = unit(0, "lines"),
plot.margin = unit(c(0, 0, 0, 0), "lines")
)
} else {
plot_theme <-
theme_bw() +
theme(panel.grid = element_blank())
theme(panel.grid = element_blank())
}

plot_range <-
Expand Down Expand Up @@ -316,21 +319,21 @@ DensityScatterPlot <- function(
# Make plot
plot <-
plot_data %>%
ggplot(aes(marker1, marker2, color = dens)) +
geom_hline(yintercept = 0, color = "gray") +
geom_vline(xintercept = 0, color = "gray") +
geom_point(
size = pt_size,
alpha = alpha,
show.legend = FALSE
) +
scale_x_continuous(limits = plot_range) +
scale_y_continuous(limits = plot_range) +
plot_theme +
labs(
x = marker1,
y = marker2
)
ggplot(aes(marker1, marker2, color = dens)) +
geom_hline(yintercept = 0, color = "gray") +
geom_vline(xintercept = 0, color = "gray") +
geom_point(
size = pt_size,
alpha = alpha,
show.legend = FALSE
) +
scale_x_continuous(limits = plot_range) +
scale_y_continuous(limits = plot_range) +
plot_theme +
labs(
x = marker1,
y = marker2
)

if (isTRUE(coord_fixed) && isFALSE(margin_density)) {
plot <- plot + coord_fixed()
Expand All @@ -340,28 +343,28 @@ DensityScatterPlot <- function(
if (!is.null(colors)) {
plot <-
plot +
scale_color_gradientn(colors = colors)
scale_color_gradientn(colors = colors)
} else {
plot <-
plot +
scale_color_viridis_c(option = "turbo")
scale_color_viridis_c(option = "turbo")
}

# Facet
if (!is.null(facet_vars)) {
if (length(facet_vars) == 1) {
plot <-
plot +
facet_grid(rows = vars(!!!syms(facet_vars)))
facet_grid(rows = vars(!!!syms(facet_vars)))
}

if (length(facet_vars) == 2) {
plot <-
plot +
facet_grid(
rows = vars(!!!syms(facet_vars[1])),
cols = vars(!!!syms(facet_vars[2]))
)
facet_grid(
rows = vars(!!!syms(facet_vars[1])),
cols = vars(!!!syms(facet_vars[2]))
)
}
}

Expand Down Expand Up @@ -528,44 +531,44 @@ DensityScatterPlot <- function(
if (isTRUE(margin_density)) {
density_plot_x <-
plot_data %>%
ggplot(aes(marker1)) +
geom_density(
fill = "lightgray",
color = NA
) +
scale_x_continuous(limits = plot_range) +
scale_y_continuous(expand = expansion(0)) +
theme_void() +
theme(
panel.spacing = unit(0, "lines"),
plot.margin = unit(c(0, 0, 0, 0), "lines")
)
ggplot(aes(marker1)) +
geom_density(
fill = "lightgray",
color = NA
) +
scale_x_continuous(limits = plot_range) +
scale_y_continuous(expand = expansion(0)) +
theme_void() +
theme(
panel.spacing = unit(0, "lines"),
plot.margin = unit(c(0, 0, 0, 0), "lines")
)

density_plot_y <-
plot_data %>%
ggplot(aes(marker2)) +
geom_density(
fill = "lightgray",
color = NA
) +
scale_x_continuous(limits = plot_range) +
scale_y_continuous(expand = expansion(0)) +
theme_void() +
theme(
panel.spacing = unit(0, "lines"),
plot.margin = unit(c(0, 0, 0, 0), "lines")
) +
coord_flip()
ggplot(aes(marker2)) +
geom_density(
fill = "lightgray",
color = NA
) +
scale_x_continuous(limits = plot_range) +
scale_y_continuous(expand = expansion(0)) +
theme_void() +
theme(
panel.spacing = unit(0, "lines"),
plot.margin = unit(c(0, 0, 0, 0), "lines")
) +
coord_flip()

plot <-
density_plot_x +
plot_spacer() +
plot +
density_plot_y +
plot_layout(
widths = c(5, 1),
heights = c(1, 5)
)
plot_spacer() +
plot +
density_plot_y +
plot_layout(
widths = c(5, 1),
heights = c(1, 5)
)
}

return(plot)
Expand Down
14 changes: 8 additions & 6 deletions man/DensityScatterPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pixelatorR.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: c4f9fa7f-dca0-4da6-b4ea-10263ff3ea00

RestoreWorkspace: No
SaveWorkspace: No
Expand Down

0 comments on commit c3a9281

Please sign in to comment.