Skip to content

Instantly share code, notes, and snippets.

@angelovangel
Last active June 23, 2023 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save angelovangel/a44d1eaafc6fcd20f1e2dd3425bb5d71 to your computer and use it in GitHub Desktop.
Save angelovangel/a44d1eaafc6fcd20f1e2dd3425bb5d71 to your computer and use it in GitHub Desktop.
FACS data plotting
# this code is about
# reading fcs files (from FACS) and converting them to one dataframe
# plotting is then done normally in ggplot
# installation instructions for the required libraries can be found on the internet:)
library(flowCore)
library(tidyverse)
### this function reads a fcs file and returns the data as a tibble, adding a column with the file name #####
read.fcs <- function(x) {
df <- read.FCS(x) %>% exprs() %>% as.tibble() %>% mutate(sample = x)
return(df)
}
################################################################################################################
# then read all files in one df, using map
# you have to be in the directory with your fcs files, e.g. use setwd("/path-to-directory")
files <- list.files(pattern = "*.fcs")
df <- map_df(files, read.fcs)
# optional, in the following line I just order the samples the way I want them in the plot
# df <- df %>% mutate(sample = factor(sample, levels = c("A06 E16 no NR.fcs", "A08 op no NR.fcs", "A07 E16 with NR.fcs", "A09 ope with NR.fcs")))
# after that you have to play with the plotting to get what you want
df %>%
ggplot() +
geom_hex(aes(`FSC-A`, `FL3-A`), bins = 128, alpha = 1) +
theme_bw() +
facet_grid(~ sample) +
scale_y_continuous(limits = c(0, 250000)) +
scale_x_continuous(limits = c(0, 1e6)) +
scale_fill_distiller(palette = "Spectral", trans = "sqrt") +
geom_abline(slope = 0.12, alpha = 0.3, color = "black")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment