plot_gates: Plot a default layout of gated populations of a set of FCS...

View source: R/plot_gates_default.R

plot_gatesR Documentation

Plot a default layout of gated populations of a set of FCS files (e.g. from one experiment)

Description

Getting an overview of the results of a flow cytometry experiment can be a generic task if gating and analysis strategy is fixed. Here, ggcyto is used to create an arrangement of pseudocolor plots. A number of default settings has been selected which may garuantee good looking plots for most cases. Upon problems, the function may be improved to handle edge cases. Currently, customization is limited to a few rather technical aspects of the plots. This may be subject to expansion but generally the range of customization is too large to fit it into a generic function with a finite number of arguments. Hence, for very specific and detailed requriements manual plotting and fiddling is unavoidable. For that, the code of this function may serve as a template.

Usage

plot_gates(
  gs,
  gates_df,
  facetting = NULL,
  plot_gates = T,
  plot_gate_names = T,
  plot_gate_pct = T,
  inverse_trans = F,
  geom = c("hex", "pointdensity", "scattermore"),
  gate_color = "black",
  gate_stats_color = "black",
  pct_digits = 1,
  plot_contours = F,
  contour_args = list(fill = "white", geom = "polygon", color = "black", contour_var =
    "ndensity", breaks = seq(0.05, 0.95, 0.1), alpha = 0.8, linewidth = 0.2),
  col_pal = RColorBrewer::brewer.pal(11, "Spectral"),
  col_pal_trans = "pseudo_log",
  theme = ggplot2::theme_bw(),
  theme_args = list(panel.grid = element_blank(), strip.background = element_rect(fill =
    "grey95", color = "white"), axis.text = element_blank(), axis.ticks =
    element_blank(), legend.position = "none"),
  ...
)

Arguments

gs

a gatingset, e.g. made with fcexpr::wsp_get_gs

gates_df

a data frame with informaion of how to plot gates, made with fcexpr::gs_get_gates

facetting

which facetting (ggplot2) to apply, facet_wrap or facet_grid with respective arguments, check flowCore::pData(gs) for valid columns; facet_null will completely remove facets; by default facetting is done across fcs each individual fcs file

plot_gates

logical whether to plot gates

plot_gate_names

logical whether to plot gate names

plot_gate_pct

logical whether to plot gate percentages (fraction of parent)

inverse_trans

logical wheter to inverse transform axes numbers; if TRUE this will make axes look like in flowjo

geom

how to plot data; recommendation is hex; hex = geom_hex taking the binwidths column of gates_df into account, pointdensity = ggpointdensity::geom_pointdensity (see ... and optionally provide max_nrow_to_plot (passed to ggcyto::ggcyto) to limit the number of dots per plot, defaults to 2000; be careful may increase time for plotting a lot), scattermore = scattermore::geom_scattermore which is a high performance dot plot version for quickly plotting millions of points (only black-white currently)

gate_color

line color of gates

gate_stats_color

font color of gate statistics

pct_digits

how many digits after comma to print; passed to 'digits' of ggcyto::geom_stats

plot_contours

logical whether to plot contour lines on top with ggplot2::geom_density_2d

col_pal

color palette to use for color gradient generation

col_pal_trans

argument passed as 'trans' in scale_fill_gradientn

Value

a list of ggplot2 objects, one for every gating level; each list index contains respective plots for every fcs file

Examples

## Not run: 
## read gatingset
gs <- fcexpr::wsp_get_gs(wsp = ws, groups = "Group1")

## write meta data to pData of gs; sd is sampledescription
p.df <-
flowCore::pData(gs) %>%
tibble::rownames_to_column("FileName") %>%
dplyr::left_join(sd) %>%
tibble::column_to_rownames("FileName")
p.df$FileName <- rownames(p.df)
flowCore::pData(gs) <- p.df

## get the gates_df, optionally select relevant gates, and modify
gates <- fcexpr::gs_get_gates(gs, n_bins = 100^2)
gates <- gates[which(gates$Population %in% c("CD8+", "CD8-")),]
gates$facet_strip <- T

## selected gates; to order facetted plot the inline factor level
## ordering is required as flowCore::pData(gs) cannot contain factors
## axis.text = element_blank() is part of ... and will omit axis numbers (passed to ggplot2::theme)
plotlist <-
fcexpr::plot_gates(gs = gs,
gates_df = gates,
facetting = facet_grid(cols = vars(factor(diluton_factor, levels = c(unique(p.df$diluton_factor)))), rows = vars(CD8_biotin_batch)),
axis.text = element_blank())

## paste plots together with patchwork and save
## patchwork is superior to cowplot as is will completely ignore ommitted facet_strips
ggsave(patchwork::wrap_plots(plotlist, ncol = 1), filename = paste0("facsplots.png"), device = "png", path = im_path, dpi = "retina", width = 18, height = 7)

## End(Not run)

Close-your-eyes/fcexpr documentation built on Sept. 29, 2023, 12:27 a.m.