6
6
# ' This function can be used for plotting a single gene expression across
7
7
# ' different groups in a study with complex group design.
8
8
# '
9
- # ' @param seu_obj A complete Seurat object
9
+ # ' @param seu_obj A complete Seurat object.
10
10
# ' @param feature Gene name. Only one gene is allowed.
11
11
# ' @param celltypes Cell types to be included in the dot plot. Default: all cell types.
12
- # ' @param groupby The group to show on x axis. One of the column names in meta.data.
12
+ # ' @param groups The group to show on x axis. One of the column names in meta.data.
13
13
# ' @param splitby The group to separate the gene expression. One of the column names in meta.data.
14
- # ' @param scale.by Methods to scale the dot size. "radius" or "size"
15
- # ' @param strip.color Colors for the strip background
14
+ # ' @param scale.by Methods to scale the dot size. "radius" or "size".
15
+ # ' @param color.palette Color for gene expression.
16
+ # ' @param strip.color Colors for the strip background.
17
+ # ' @param font.size Font size for the labels.
16
18
# ' @param do.scale Whether or not to scale the dot when percentage expression of the gene is less than 20.
17
19
# ' @return A ggplot object
18
20
# ' @export
19
21
complex_dotplot_single <- function (
20
22
seu_obj ,
21
23
feature ,
22
24
celltypes = NULL ,
23
- groupby ,
25
+ groups ,
24
26
splitby = NULL ,
27
+ color.palette = NULL ,
28
+ font.size = 12 ,
25
29
strip.color = NULL ,
26
30
do.scale = T ,
27
31
scale.by = ' radius'
28
32
){
29
- groupby_level <- levels(seu_obj @ meta.data [,groupby ])
30
- if (is.null(groupby_level )){
31
- seu_obj @ meta.data [,groupby ] <- factor (seu_obj @ meta.data [,groupby ], levels = names(table(seu_obj @ meta.data [,groupby ])))
32
- groupby_level <- levels(seu_obj @ meta.data [,groupby ])
33
+ groups_level <- levels(seu_obj @ meta.data [,groups ])
34
+ if (is.null(groups_level )){
35
+ seu_obj @ meta.data [,groups ] <- factor (seu_obj @ meta.data [,groups ], levels = names(table(seu_obj @ meta.data [,groups ])))
36
+ groups_level <- levels(seu_obj @ meta.data [,groups ])
33
37
}
34
- if (sum(grepl(" _" , groupby_level ))> 0 ){
35
- seu_obj @ meta.data [,groupby ]<- gsub(" _" ," -" ,seu_obj @ meta.data [,groupby ])
36
- groupby_level <- gsub(" _" ," -" ,groupby_level )
37
- seu_obj @ meta.data [,groupby ] <- factor (seu_obj @ meta.data [,groupby ], levels = groupby_level )
38
- }
39
38
if (is.null(celltypes )){
40
39
celltypes <- levels(seu_obj )
41
40
}
42
- seu_obj <- subset(seu_obj ,idents = celltypes )
43
- celltypes <- gsub(" _" , " -" , celltypes )
44
- seu_obj @ meta.data $ celltype <- as.character(seu_obj @ active.ident )
45
- seu_obj @ meta.data $ celltype <- gsub(" _" , " -" , seu_obj @ meta.data $ celltype )
46
- seu_obj <- SetIdent(seu_obj , value = ' celltype' )
47
- levels(seu_obj )<- celltypes
41
+ if (is.null(color.palette )){
42
+ color.palette <- colorRampPalette(c(' grey80' ,' lemonchiffon1' ,' indianred1' ,' darkred' ))(255 )
43
+ }
48
44
if (! is.null(splitby )){
49
45
if (is.null(levels(seu_obj @ meta.data [,splitby ]))){
50
46
seu_obj @ meta.data [,splitby ] <- factor (seu_obj @ meta.data [,splitby ], levels = names(table(seu_obj @ meta.data [,splitby ])))
51
47
}
52
48
splitby_level <- levels(seu_obj @ meta.data [,splitby ])
53
- count_df <- extract_gene_count(seu_obj , features = feature , meta.groups = c(groupby ,splitby ))
54
- count_df $ new_group <- paste(count_df [,groupby ], count_df [," celltype" ], count_df [,splitby ],sep = " ___" )
55
- exp_df <- aggregate(. ~ new_group , data = count_df [,c(' new_group' ,feature )], FUN = function (x ){mean(expm1(x ))})
56
- pct_df <- aggregate(. ~ new_group , data = count_df [,c(' new_group' ,feature )], FUN = function (x ){length(x [x > 0 ]) / length(x )})
49
+ count_df <- extract_gene_count(seu_obj , features = feature , cell.types = celltypes , meta.groups = c(groups ,splitby ))
50
+ count_df $ new_group <- paste(count_df [,groups ], count_df [," celltype" ], count_df [,splitby ],sep = " ___" )
51
+ exp_df <- aggregate(. ~ new_group , data = count_df [,c(' new_group' ,feature )], FUN = function (x ){mean(expm1(x ))})
52
+ pct_df <- aggregate(. ~ new_group , data = count_df [,c(' new_group' ,feature )], FUN = function (x ){length(x [x > 0 ]) / length(x )}) # This is the same data processing as Seurat
57
53
colnames(exp_df )[2 ]<- " avg.exp"
58
54
colnames(pct_df )[2 ]<- " pct.exp"
59
55
data_plot <- merge(exp_df , pct_df , by = ' new_group' )
60
- data_plot $ groupby <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " ___" ),FUN = function (x ){x [[1 ]]}))
56
+ data_plot $ groups <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " ___" ),FUN = function (x ){x [[1 ]]}))
61
57
data_plot $ celltype <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " ___" ),FUN = function (x ){x [[2 ]]}))
62
58
data_plot $ splitby <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " ___" ),FUN = function (x ){x [[3 ]]}))
63
- data_plot $ groupby <- factor (data_plot $ groupby , levels = groupby_level )
59
+ data_plot $ groups <- factor (data_plot $ groups , levels = groups_level )
64
60
data_plot $ splitby <- factor (data_plot $ splitby , levels = splitby_level )
65
61
data_plot $ celltype <- factor (data_plot $ celltype , levels = rev(celltypes ))
66
62
} else {
67
- count_df <- extract_gene_count(seu_obj , features = feature , meta.groups = groupby )
68
- count_df $ new_group <- paste(count_df [,groupby ], count_df [," celltype" ],sep = " _ " )
63
+ count_df <- extract_gene_count(seu_obj , features = feature , cell.types = celltypes , meta.groups = groups )
64
+ count_df $ new_group <- paste(count_df [,groups ], count_df [," celltype" ],sep = " ___ " )
69
65
exp_df <- aggregate(. ~ new_group , data = count_df [,c(' new_group' ,feature )], FUN = function (x ){mean(expm1(x ))})
70
66
pct_df <- aggregate(. ~ new_group , data = count_df [,c(' new_group' ,feature )], FUN = function (x ){length(x [x > 0 ]) / length(x )})
71
67
colnames(exp_df )[2 ]<- " avg.exp"
72
68
colnames(pct_df )[2 ]<- " pct.exp"
73
69
data_plot <- merge(exp_df , pct_df , by = ' new_group' )
74
- data_plot $ groupby <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " _ " ),FUN = function (x ){x [[1 ]]}))
75
- data_plot $ celltype <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " _ " ),FUN = function (x ){x [[2 ]]}))
76
- data_plot $ groupby <- factor (data_plot $ groupby , levels = groupby_level )
77
- data_plot $ celltype <- factor (data_plot $ celltype , levels = celltypes )
70
+ data_plot $ groups <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " ___ " ),FUN = function (x ){x [[1 ]]}))
71
+ data_plot $ celltype <- as.character(lapply(X = strsplit(data_plot $ new_group , split = " ___ " ),FUN = function (x ){x [[2 ]]}))
72
+ data_plot $ groups <- factor (data_plot $ groups , levels = groups_level )
73
+ data_plot $ celltype <- factor (data_plot $ celltype , levels = rev( celltypes ) )
78
74
}
79
75
scale.func <- switch (
80
76
EXPR = scale.by ,
81
77
' size' = scale_size ,
82
78
' radius' = scale_radius ,
83
79
stop(" 'scale.by' must be either 'size' or 'radius'" )
84
- )
80
+ ) # ## This function is from Seurat https://github.com/satijalab/seurat
85
81
data_plot $ pct.exp <- round(100 * data_plot $ pct.exp , 2 )
86
82
data_plot $ avg.exp <- scale(data_plot $ avg.exp )
87
- p <- ggplot(data_plot , aes(y = celltype , x = groupby )) +
83
+ p <- ggplot(data_plot , aes(y = celltype , x = groups )) +
88
84
geom_tile(fill = " white" , color = " white" ) +
89
85
geom_point(aes( colour = avg.exp , size = pct.exp )) +
90
- scale_color_gradientn(colours = colorRampPalette(c( ' grey80 ' , ' lemonchiffon1 ' , ' indianred1 ' , ' darkred ' ))( 255 ) )+
86
+ scale_color_gradientn(colours = color.palette )+
91
87
theme(panel.background = element_rect(fill = " white" , colour = " black" ),
92
- axis.text.x = element_text(angle = 45 , hjust = 1 ),
93
- plot.title = element_text(size = 16 , hjust = 0.5 , face = ' bold' ),
94
- axis.text = element_text(size = 12 ),
95
- axis.title = element_text(size = 8 ),
96
- legend.text = element_text(size = 8 ),
97
- legend.title = element_text(size = 12 ),
88
+ axis.text.x = element_text(angle = 45 , hjust = 1 , size = font.size ),
89
+ plot.title = element_text(size = ( font.size + 2 ), hjust = 0.5 , face = ' bold' ),
90
+ axis.text = element_text(size = font.size ),
91
+ legend.text = element_text(size = ( font.size - 2 ) ),
92
+ legend.title = element_text(size = ( font.size ) ),
93
+ strip.text = element_text( size = font.size ),
98
94
legend.position = " right" )+
99
95
ylab(" " )+ xlab(" " )+ ggtitle(feature )
100
96
if (do.scale ){
@@ -126,15 +122,15 @@ complex_dotplot_single <- function(
126
122
# ' @param seu_obj A complete Seurat object
127
123
# ' @param features A vector of gene names.
128
124
# ' @param celltypes Cell types to be included in the dot plot. Default: all cell types.
129
- # ' @param groupby Group ID Must be one of the column names in the meta.data slot of the Seurat object.
125
+ # ' @param groups Group ID Must be one of the column names in the meta.data slot of the Seurat object.
130
126
# ' @param strip.color Colors for the strip background
131
127
# ' @return A ggplot object
132
128
# ' @export
133
129
complex_dotplot_multiple <- function (
134
130
seu_obj ,
135
131
features ,
136
132
celltypes = NULL ,
137
- groupby ,
133
+ groups ,
138
134
strip.color = NULL
139
135
){
140
136
pb <- progress_bar $ new(
@@ -143,7 +139,7 @@ complex_dotplot_multiple <- function(
143
139
plot_list <- list ()
144
140
for (i in 1 : length(features )){
145
141
pp <- invisible (
146
- complex_dotplot_single(seu_obj = seu_obj , feature = features [i ], groupby = groupby , celltypes = celltypes )
142
+ complex_dotplot_single(seu_obj = seu_obj , feature = features [i ], groups = groups , celltypes = celltypes )
147
143
)
148
144
pp <- pp $ data
149
145
pp $ gene <- features [i ]
@@ -155,7 +151,7 @@ complex_dotplot_multiple <- function(
155
151
all_data $ gene <- factor (all_data $ gene , levels = rev(features ))
156
152
all_data $ celltype <- factor (all_data $ celltype , levels = levels(seu_obj ))
157
153
p <- invisible (
158
- ggplot(all_data , aes(x = groupby , y = gene )) +
154
+ ggplot(all_data , aes(x = groups , y = gene )) +
159
155
geom_tile(fill = " white" , color = " white" ) +
160
156
geom_point(aes( colour = avg.exp , size = pct.exp ), alpha = 0.9 ) +
161
157
scale_color_gradientn(colours = grDevices :: colorRampPalette(c(' grey80' ,' lemonchiffon1' ,' indianred1' ,' darkred' ))(255 ))+
0 commit comments