View source: R/dittoFreqPlot.R
dittoFreqPlot | R Documentation |
Plot cell type/cluster/identity frequencies per sample and per grouping
dittoFreqPlot(
object,
var,
sample.by = NULL,
group.by,
color.by = group.by,
vars.use = NULL,
scale = c("percent", "count"),
max.normalize = FALSE,
plots = c("boxplot", "jitter"),
split.nrow = NULL,
split.ncol = NULL,
split.adjust = list(),
cells.use = NULL,
data.out = FALSE,
do.hover = FALSE,
color.panel = dittoColors(),
colors = seq_along(color.panel),
y.breaks = NULL,
min = 0,
max = NA,
var.labels.rename = NULL,
var.labels.reorder = NULL,
x.labels = NULL,
x.labels.rotate = TRUE,
x.reorder = NULL,
theme = theme_classic(),
xlab = group.by,
ylab = "make",
main = "make",
sub = NULL,
jitter.size = 1,
jitter.width = 0.2,
jitter.color = "black",
jitter.position.dodge = boxplot.position.dodge,
do.raster = FALSE,
raster.dpi = 300,
boxplot.width = 0.4,
boxplot.color = "black",
boxplot.show.outliers = NA,
boxplot.outlier.size = 1.5,
boxplot.fill = TRUE,
boxplot.position.dodge = vlnplot.width,
boxplot.lineweight = 1,
vlnplot.lineweight = 1,
vlnplot.width = 1,
vlnplot.scaling = "area",
vlnplot.quantiles = NULL,
ridgeplot.lineweight = 1,
ridgeplot.scale = 1.25,
ridgeplot.ymax.expansion = NA,
ridgeplot.shape = c("smooth", "hist"),
ridgeplot.bins = 30,
ridgeplot.binwidth = NULL,
add.line = NULL,
line.linetype = "dashed",
line.color = "black",
legend.show = TRUE,
legend.title = color.by
)
object |
A Seurat, SingleCellExperiment, or SummarizedExperiment object. |
var |
String name of a metadata that contains discrete data, or a factor or vector containing such data for all cells/samples in the target |
sample.by |
String name of a metadata containing which samples each cell belongs to. Note that when this is not provided, there will only be one data point per grouping.
A warning can be expected then for all |
group.by |
String representing the name of a metadata to use for separating the cells/samples into discrete groups. |
color.by |
String representing the name of a metadata to use for setting fills.
Great for highlighting supersets or subgroups when wanted, but it defaults to |
vars.use |
String or string vector naming a subset of the values of Hint: use Note: When |
scale |
"count" or "percent". Sets whether data should be shown as counts versus percentage. |
max.normalize |
Logical which sets whether the data for each When set to |
plots |
String vector which sets the types of plots to include: possibilities = "jitter", "boxplot", "vlnplot", "ridgeplot". Order matters: c("vlnplot", "boxplot", "jitter") will put a violin plot in the back, boxplot in the middle, and then individual dots in the front. See details section for more info. |
split.nrow , split.ncol |
Integers which set the dimensions of the facet grid. (the |
split.adjust |
A named list which allows extra parameters to be pushed through to the faceting function call. List elements should be valid inputs to the faceting functions, e.g. 'list(scales = "free")'. Faceting for this dittoFreqPlot is always by the |
cells.use |
String vector of cells'(/samples' for bulk data) names OR an integer vector specifying the indices of cells/samples which should be included. Alternatively, a Logical vector, the same length as the number of cells in the object, which sets which cells to include. |
data.out |
Logical. When set to |
do.hover |
Logical. Default = |
color.panel |
String vector which sets the colors to draw from for plot fills.
Default = |
colors |
Integer vector, the indexes / order, of colors from color.panel to actually use.
(Provides an alternative to directly modifying |
y.breaks |
Numeric vector, a set of breaks that should be used as major gridlines. c(break1,break2,break3,etc.). |
min , max |
Scalars which control the zoom of the plot. These inputs set the minimum / maximum values of the data to display. Default = NA, which allows ggplot to set these limits based on the range of all data being shown. |
var.labels.rename |
String vector for renaming the distinct identities of Hint: use |
var.labels.reorder |
Integer vector. A sequence of numbers, from 1 to the number of distinct Method: Make a first plot without this input.
Then, treating the top-left-most grouping as index 1, and the bottom-right-most as index n.
Values of |
x.labels |
String vector, c("label1","label2","label3",...) which overrides the names of groupings. |
x.labels.rotate |
Logical which sets whether the labels should be rotated.
Default: |
x.reorder |
Integer vector. A sequence of numbers, from 1 to the number of groupings, for rearranging the order of x-axis groupings. Method: Make a first plot without this input. Then, treating the leftmost grouping as index 1, and the rightmost as index n. Values of x.reorder should be these indices, but in the order that you would like them rearranged to be. Recommendation for advanced users: If you find yourself coming back to this input too many times, an alternative solution that can be easier long-term
is to make the target data into a factor, and to put its levels in the desired order: |
theme |
A ggplot theme which will be applied before dittoSeq adjustments.
Default = |
xlab |
String which sets the grouping-axis label (=x-axis for box and violin plots, y-axis for ridgeplots).
Set to |
ylab |
String, sets the continuous-axis label (=y-axis for box and violin plots, x-axis for ridgeplots). Default = "make" and if left as make, a title will be automatically generated. |
main |
String, sets the plot title. Default = "make" and if left as make, a title will be automatically generated. To remove, set to |
sub |
String, sets the plot subtitle |
jitter.size |
Scalar which sets the size of the jitter shapes. |
jitter.width |
Scalar that sets the width/spread of the jitter in the x direction. Ignored in ridgeplots. Note for when |
jitter.color |
String which sets the color of the jitter shapes |
jitter.position.dodge |
Scalar which adjusts the relative distance between jitter widths when multiple subgroups exist per |
do.raster |
Logical. When set to |
raster.dpi |
Number indicating dots/pixels per inch (dpi) to use for rasterization. Default = 300. |
boxplot.width |
Scalar which sets the width/spread of the boxplot in the x direction |
boxplot.color |
String which sets the color of the lines of the boxplot |
boxplot.show.outliers |
Logical, whether outliers should by including in the boxplot.
Default is |
boxplot.outlier.size |
Scalar which adjusts the size of points used to mark outliers |
boxplot.fill |
Logical, whether the boxplot should be filled in or not. Known bug: when boxplot fill is turned off, outliers do not render. |
boxplot.position.dodge |
Scalar which adjusts the relative distance between boxplots when multiple are drawn per grouping (a.k.a. when |
boxplot.lineweight |
Scalar which adjusts the thickness of boxplot lines. |
vlnplot.lineweight |
Scalar which sets the thickness of the line that outlines the violin plots. |
vlnplot.width |
Scalar which sets the width/spread of violin plots in the x direction |
vlnplot.scaling |
String which sets how the widths of the of violin plots are set in relation to each other.
Options are "area", "count", and "width". If the default is not right for your data, I recommend trying "width".
For an explanation of each, see |
vlnplot.quantiles |
Single number or numeric vector of values in [0,1] naming quantiles at which to draw a horizontal line within each violin plot. Example: |
ridgeplot.lineweight |
Scalar which sets the thickness of the ridgeplot outline. |
ridgeplot.scale |
Scalar which sets the distance/overlap between ridgeplots. A value of 1 means the tallest density curve just touches the baseline of the next higher one. Higher numbers lead to greater overlap. Default = 1.25 |
ridgeplot.ymax.expansion |
Scalar which adjusts the minimal space between the top-most grouping and the top of the plot in order to ensure that the curve is not cut off by the plotting grid. The larger the value, the greater the space requested. When left as NA, dittoSeq will attempt to determine an ideal value itself based on the number of groups & linear interpolation between these goal posts: 0.6 when g<=3, 0.1 when g==12, and 0.05 when g>=34, where g is the number of groups. |
ridgeplot.shape |
Either "smooth" or "hist", sets whether ridges will be smoothed (the typical, and default) versus rectangular like a histogram.
(Note: as of the time shape "hist" was added, combination of jittered points is not supported by the |
ridgeplot.bins |
Integer which sets how many chunks to break the x-axis into when |
ridgeplot.binwidth |
Integer which sets the width of chunks to break the x-axis into when |
add.line |
numeric value(s) where one or multiple line(s) should be added |
line.linetype |
String which sets the type of line for |
line.color |
String that sets the color(s) of the |
legend.show |
Logical. Whether the legend should be displayed. Default = |
legend.title |
String or |
The function creates a dataframe containing counts and percent makeup of var
identities per sample if sample.by
is given, or per group if only group.by
is given.
color.by
can optionally be used to add subgroupings to calculations and ultimate plots, or to convey super-groups of group.by
groupings.
Typically, var
will be pointed to clustering or cell type annotations, but in truth it can be given any discrete data.
If a set of cells to use is indicated with the cells.use
input, only those cells/samples are used for counts and percent makeup calculations.
If a set of var
-values to show is indicated with the vars.use
input, the data.frame is trimmed at the end to include only corresponding rows.
If max.normalized
is set to TRUE
, counts and percent data are transformed to a 0-1 scale, which makes better use of white space for lower frequency var
-values.
Either percent of total (scale = "percent"
), which is the default, or counts (if scale = "count"
)
data is then (gg)plotted with the data representation types in plots
by utilizing the same machinery as dittoPlot
.
Faceting by var
-data values is utilized to achieve per var
-value (e.g. cluster or cell type) granularity.
See below for additional customization options!
A ggplot plot where frequencies of discrete data, grouped by sample, condition, etc., is shown on the y-axis by a violin plot, boxplot, and/or jittered points, or on the x-axis by a ridgeplot with or without jittered points.
Alternatively, if data.out = TRUE
, a list containing the plot ("p") and a dataframe of the underlying data ("data").
Alternatively, if do.hover = TRUE
, a plotly conversion of the ggplot output in which underlying data can be retrieved upon hovering the cursor over the plot.
The function is restricted in that each samples' cells, indicated by the unique values of sample.by
-data, must exist within single group.by
and color.by
groupings.
Thus, in order to ensure all valid var
-data composition data points are generated, prior to calculations...
var
-data are ensured to be a factor, which ensures a calculation will be run for every var
-value (a.k.a. cell type or cluster)
group.by
-data and color-by
-data are treated as non-factor data, which ensures that calculations are run only for the groupings that each sample is associated with.
The plots
argument determines the types of data representation that will be generated, as well as their order from back to front.
Options are "jitter"
, "boxplot"
, "vlnplot"
, and "ridgeplot"
.
Each plot type has specific associated options which are controlled by variables that start with their associated string.
For example, all jitter adjustments start with "jitter.
", such as jitter.size
and jitter.width
.
Inclusion of "ridgeplot"
overrides "boxplot"
and "vlnplot"
presence and changes the plot to be horizontal.
Additionally:
Colors can be adjusted with color.panel
.
Subgroupings: color.by
can be utilized to split major group.by
groupings into subgroups.
When this is done in y-axis plotting, dittoSeq automatically ensures the centers of all geoms will align,
but users will need to manually adjust jitter.width
to less than 0.5/num_subgroups to avoid overlaps.
There are also three inputs through which one can use to control geom-center placement, but the easiest way to do all at once so is to just adjust vlnplot.width
!
The other two: boxplot.position.dodge
, and jitter.position.dodge
.
Line(s) can be added at single or multiple value(s) by providing these values to add.line
.
Linetype and color are set with line.linetype
, which is "dashed" by default, and line.color
, which is "black" by default.
Titles and axes labels can be adjusted with main
, sub
, xlab
, ylab
, and legend.title
arguments.
The legend can be hidden by setting legend.show = FALSE
.
y-axis zoom and tick marks can be adjusted using min
, max
, and y.breaks
.
x-axis labels and groupings can be changed / reordered using x.labels
and x.reorder
, and rotation of these labels can be turned on/off with x.labels.rotate = TRUE/FALSE
.
Daniel Bunis
dittoBarPlot
for a data representation that emphasizes total makeup of samples/groups rather than focusing on the var
-data values individually.
# Establish some workable example data
example(importDittoBulk, echo = FALSE)
myRNA1 <- myRNA
colnames(myRNA) <- paste0(colnames(myRNA),"_1")
example(importDittoBulk, echo = FALSE)
myRNA <- cbind(myRNA, myRNA1)
myRNA <- setBulk(myRNA, FALSE)
myRNA$sample <- rep(1:12, each = 10)
myRNA$groups <- rep(c("A", "B"), each = 60)
myRNA$subgroups <- rep(as.character(c(1:3,1:3,1:3,1:3)), each = 10)
myRNA
# There are three main inputs for this function, in addition to 'object'.
# var = typically this will be cell types annotations or clustering
# sample.by = the name of a metadata containing sample assignment of cells.
# group.by = how to group the data on the x-axis (y-axis for ridgeplots)
dittoFreqPlot(myRNA,
var = "clustering",
sample.by = "sample",
group.by = "groups")
# 'color.by' can also be set differently from 'group.by' to have the effect
# of highlighting supersets or subgroupings:
dittoFreqPlot(myRNA, "clustering",
group.by = "groups",
sample.by = "sample",
color.by = "subgroups")
# The var-values shown can be subset with 'vars.use'
dittoFreqPlot(myRNA, "clustering",
group.by = "groups", sample.by = "sample", color.by = "subgroups",
vars.use = 1:2)
# Lower frequency groups can be expanded to use the entire y-axis by:
# turning on 'max.normalize'-ation:
dittoFreqPlot(myRNA, "clustering",
group.by = "groups", sample.by = "sample", color.by = "subgroups",
max.normalize = TRUE)
# or by setting y-scale limits to be set by the contents of facets:
dittoFreqPlot(myRNA, "clustering",
group.by = "groups", sample.by = "sample", color.by = "subgroups",
split.adjust = list(scales = "free_y"))
# Data representations can also be selected and reordered with the 'plots'
# input, and further adjusted with inputs applying to each representation.
dittoFreqPlot(myRNA,
var = "clustering", sample.by = "sample", group.by = "groups",
plots = c("vlnplot", "boxplot", "jitter"),
vlnplot.lineweight = 0.2,
boxplot.fill = FALSE,
boxplot.lineweight = 0.2)
# Finally, 'sample.by' is not technically required. When not given, a
# single-datapoint of overall composition stats will be shown for each
# grouping.
# Just note, all data representation other than "jitter" will complain
# due to there only being the one datapoint per group.
dittoFreqPlot(myRNA,
var = "clustering", group.by = "groups", color.by = "subgroups",
plots = "jitter")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.