Skip to content

Commit

Permalink
plotColDataFreqpoly
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamoses committed Feb 23, 2023
1 parent 7147639 commit eca441e
Show file tree
Hide file tree
Showing 13 changed files with 619 additions and 41 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: Voyager
Type: Package
Title: From geospatial to spatial omics
Version: 1.0.9
Version: 1.0.10
Authors@R:
c(person("Lambda", "Moses", email = "[email protected]",
role = c("aut", "cre"),
Expand All @@ -22,7 +22,7 @@ Imports:
BiocParallel,
bluster,
ggnewscale,
ggplot2,
ggplot2 (>= 3.4.0),
Matrix,
methods,
patchwork,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export(moranPlot)
export(plotAnnotGraph)
export(plotCellBin2D)
export(plotColDataBin2D)
export(plotColDataFreqpoly)
export(plotColDataHistogram)
export(plotColGraph)
export(plotCorrelogram)
export(plotDimLoadings)
export(plotLocalResult)
export(plotMoranMC)
export(plotRowDataBin2D)
export(plotRowDataFreqpoly)
export(plotRowDataHistogram)
export(plotSpatialFeature)
export(rowFeatureData)
Expand Down
57 changes: 56 additions & 1 deletion R/plot-non-spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,14 @@ plotRowDataBin2D <- .plot_dimdata_bin2d_fun(rowData)
#' \code{rowData} to fill the histogram.
#' @param subset Name of a logical column to only plot a subset of the data.
#' @return A ggplot object
#' @seealso plotColDataFreqpoly
#' @importFrom rlang %||% .data
#' @export
#' @examples
#' library(SFEData)
#' sfe <- McKellarMuscleData()
#' plotColDataHistogram(sfe, c("nCounts", "nGenes"), fill_by = "in_tissue",
#' bins = 50)
#' bins = 50, position = "stack")
#' plotColDataHistogram(sfe, "nCounts", subset = "in_tissue")
#' sfe2 <- sfe[, sfe$in_tissue]
#' plotColDataHistogram(sfe2, c("nCounts", "nGenes"), bins = 50)
Expand All @@ -252,3 +253,57 @@ plotColDataHistogram <- .plot_dimdata_hist(colData)
#' @rdname plotColDataHistogram
#' @export
plotRowDataHistogram <- .plot_dimdata_hist(rowData)

.plot_dimdata_freqpoly <- function(fun) {
function(sfe, feature, color_by = NULL, subset = NULL, bins = 100,
binwidth = NULL, linewidth = 1.2,
scales = "free", ncol = 1, position = "identity") {
df <- as.data.frame(fun(sfe))[, c(feature, color_by, subset), drop = FALSE]
if (!is.null(subset)) df <- df[df[[subset]],]
p <- ggplot()
if (length(feature) > 1L) {
df <- reshape(df, varying = feature, direction = "long",
v.names = "values", timevar = "variable",
times = feature)
p <- p +
geom_freqpoly(data = df,
mapping = aes(!!!syms(c(x = "values", color = color_by))),
bins = bins, linewidth = linewidth,
binwidth = binwidth, position = position) +
facet_wrap(~ variable, scales = scales, ncol = ncol)
} else {
p <- p +
geom_freqpoly(data = df,
mapping = aes(!!!syms(c(x = feature, color = color_by))),
bins = bins, linewidth = linewidth,
binwidth = binwidth, position = position)
}
p <- p + scale_color_manual(values = ditto_colors)
p
}
}

#' Plot frequency polygons for colData and rowData columns
#'
#' This function is recommended instead of \code{\link{plotColDataHistogram}}
#' when coloring by multiple categories and log transforming the y axis, which
#' causes problems in stacked histograms.
#'
#' @inheritParams ggplot2::geom_freqpoly
#' @inheritParams plotColDataHistogram
#' @param linewidth Line width of the polygons, defaults to a thicker 1.2.
#' @seealso plotColDataHistogram
#' @export
#' @examples
#' library(SFEData)
#' sfe <- McKellarMuscleData()
#' plotColDataFreqpoly(sfe, c("nCounts", "nGenes"), fill_by = "in_tissue",
#' bins = 50)
#' plotColDataFreqpoly(sfe, "nCounts", subset = "in_tissue")
#' sfe2 <- sfe[, sfe$in_tissue]
#' plotColDataFreqpoly(sfe2, c("nCounts", "nGenes"), bins = 50)
plotColDataFreqpoly <- .plot_dimdata_freqpoly(colData)

#' @rdname plotColDataFreqpoly
#' @export
plotRowDataFreqpoly <- .plot_dimdata_freqpoly(rowData)
87 changes: 87 additions & 0 deletions man/plotColDataFreqpoly.Rd

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

3 changes: 3 additions & 0 deletions man/plotColDataHistogram.Rd

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

112 changes: 112 additions & 0 deletions tests/testthat/_snaps/plot/coldata-freqpoly-multiple-variables.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit eca441e

Please sign in to comment.