Commit d0401c32 authored by Guangchuang Yu's avatar Guangchuang Yu
Browse files

update

parent a37ff1a0
Loading
Loading
Loading
Loading
+133 −0
Original line number Diff line number Diff line
---
title: "Visualizing single cell data"
author: 
- name: Guangchuang Yu
  email: guangchuangyu@gmail.com
  affiliation: Department of Bioinformatics, School of Basic Medical Sciences, Southern Medical University
date: "`r Sys.Date()`"
output:
  prettydoc::html_pretty:
    theme: cayman
    highlight: github
  pdf_document:
    toc: true
vignette: >
  %\VignetteIndexEntry{Visualizing single cell data}
  %\VignetteEngine{knitr::rmarkdown}
  %\usepackage[utf8]{inputenc}
  %\VignetteEncoding{UTF-8}
---

```{r style, echo=FALSE, results="asis", message=FALSE}
knitr::opts_chunk$set(tidy = FALSE,
                      warning = FALSE,
                      message = FALSE, 
                      fig.width = 9,
                      fig.height = 6)
library(Seurat)
library(ggplot2)
library(scplot)                      
```


```{r}
library(Seurat)
dir = "filtered_gene_bc_matrices/hg19"

pbmc.data <- Read10X(data.dir = dir)
pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells=3, min.features=200)
pbmc

## Normalizing the data
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", 
                      scale.factor = 10000)

pbmc <- NormalizeData(pbmc)

## Identify the 2000 most highly variable genes
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)

## In addition we scale the data
all.genes <- rownames(pbmc)
pbmc <- ScaleData(pbmc, features = all.genes)

pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc), 
               verbose = FALSE)
pbmc <- FindNeighbors(pbmc, dims = 1:10, verbose = FALSE)
pbmc <- FindClusters(pbmc, resolution = 0.5, verbose = FALSE)
pbmc <- RunUMAP(pbmc, dims = 1:10, umap.method = "uwot", metric = "cosine")

## Assigning cell type identity to clusters
new.cluster.ids <- c("Naive CD4 T", "CD14+ Mono", "Memory CD4 T", "B", "CD8 T",
                     "FCGR3A+ Mono", "NK", "DC", "Platelet")
names(new.cluster.ids) <- levels(pbmc)
pbmc <- RenameIdents(pbmc, new.cluster.ids)
```

## Dimensional reduction plot
```{r fig.width=9, fig.height=6}
DimPlot(pbmc, reduction = "umap",
        label = TRUE, pt.size = 0.5) 

library(ggplot2)
library(scplot)
sc_dim(pbmc) + sc_dim_geom_label()


sc_dim(pbmc) + 
    sc_dim_geom_label(geom = shadowtext::geom_shadowtext, 
            color='black', bg.color='white')
```

## Visualize 'features' on a dimensional reduction plot

```{r}
features = c("MS4A1", "GNLY", "CD3E", 
            "CD14", "FCER1A", "FCGR3A", 
            "LYZ", "PPBP", "CD8A")
FeaturePlot(pbmc,'CD4')
FeaturePlot(pbmc, features)


sc_feature(pbmc, 'CD4')
sc_feature(pbmc, features)
```

Here is the real 'features' on dimensional plot


```{r}
sc_dim(pbmc) + 
   sc_dim_geom_feature(pbmc, 'CD4', color='black')

sc_dim(pbmc, alpha=.3) + 
    ggnewscale::new_scale_color() + 
    sc_dim_geom_feature(pbmc, features, mapping=aes(color=features)) +
    scale_color_viridis_d()
```    

## Dot plot

```{r eval=FALSE}
DotPlot(pbmc, features = c("MS4A1", "GNLY", "CD3E", 
                           "CD14", "FCER1A", "FCGR3A", 
                           "LYZ", "PPBP", "CD8A"),
        group.by = 'seurat_clusters')
```        


## Violin plot of gene expression

```{r}
VlnPlot(pbmc,'CD4')

sc_violin(pbmc, 'CD4')

## allows applying an user-defined function to transform/filter the data
sc_violin(pbmc, 'CD4', .fun=function(d) dplyr::filter(d, value > 0))
```

```{r fig.height=9}
VlnPlot(pbmc, features)
sc_violin(pbmc, features)
```

index.html

0 → 100644
+272 −0

File added.

Preview size limit exceeded, changes collapsed.