-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlotting_Functions.R
109 lines (88 loc) · 3.76 KB
/
Plotting_Functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
## Heatmap Colour Gradient Scale
library(RColorBrewer)
plotto_color_gradient_blue_red = rev(brewer.pal(n = 10, name = "RdYlBu"))
## Load Arial
library(extrafont)
font_import(paths = "/home/jsch0032/projects/Ethan_iBlastoids/data", pattern = "arial.ttf")
loadfonts()
## Size parameters
stroke = 0.3
size = 0.15
lines = 0.25
## Custom theme for panels with small text
plotto_theme_panel<- function(){
font <- "Arial" #assign font family up front
theme_classic() %+replace% #replace elements we want to change
theme(
#text elements
plot.title = element_text( #title
family = font, #set font family
size = 10, #set font size
face = 'bold', #bold typeface
hjust = 0), #raise slightly
axis.title = element_text( #axis titles
family = font, #font family
size = 7), #font size
axis.text = element_text( #axis text
family = font, #axis famuly
size = 6), #font size
legend.text = element_text( #legend items
family = font, #font family
size = 7), #font size
legend.title = element_text(family = font, size=8),
line = element_line(size=lines)
)
}
## Single Cell Plotting Functions
library(Seurat)
library(ggplot2)
plotto_marker_plot = function(m, seurat, reduction = "umap", size=0.15) {
Embeddings(seurat, reduction = reduction) %>% as_tibble() %>%
mutate(g = as.matrix(seurat@assays[[[email protected]]][m,])[1,]) -> pdat
components = names(pdat)[1:2]
cnames = gsub("_","",components)
ggplot() +
geom_point(data = pdat[pdat$g<0,], aes_string(components[1], components[2]), stroke=stroke, size=size, colour="lightgrey") +
geom_point(data = pdat[pdat$g>=0,], aes_string(components[1], components[2], color="g"), stroke=stroke, size=size) +
# scale_color_gradientn(colours = rev(brewer.pal(n = 7, name =
# "RdYlBu"))) +
scale_color_gradientn(colours = c("lightgrey", rev(brewer.pal(n = 11, name =
"Spectral")[1:5]))) +
plotto_theme_panel() +
theme(legend.position = "bottom", legend.key.height = unit(0.5,"line"),
legend.spacing.x = unit(0.2, 'cm'),
legend.box.margin = margin(t=-0.4, unit = "cm")) +
labs(x=cnames[1], y=cnames[2], colour=m) -> p
p
}
plotto_signature_scoring_plot = function(sig, seurat, reduction = "umap", size=0.15) {
# components = paste(ifelse(reduction=="umap", "UMAP", "PC"), 1:2, sep="_")
# cnames = paste(ifelse(reduction=="umap", "UMAP", "PC"), 1:2, sep="")
components = colnames(Embeddings(seurat, reduction = reduction))[1:2]
cnames = gsub("_","",components)
Embeddings(seurat, reduction = reduction) %>% as_tibble() %>%
mutate(s = [email protected][,sig]) %>%
ggplot(., aes_string(components[1], components[2])) +
geom_point(aes(colour=s), stroke=stroke, size=size) +
scale_color_gradientn(colours = rev(brewer.pal(n = 10, name =
"RdYlBu"))) +
plotto_theme_panel() +
theme(legend.position = "bottom", legend.key.height = unit(0.5,"line"),
legend.spacing.x = unit(0.2, 'cm'),
line = element_line(size=lines),
legend.box.margin = margin(t=-0.4, unit = "cm")) +
labs(x=cnames[1], y=cnames[2], colour=sig) -> p
p
}
## Arrange Multiple Plots in a grid, setting the panel size
#library(gridExtra)
library(egg)
plotto_panel_it = function(plot_list, width = 2.5, height = 2.5, nrow=1) {
grid.arrange(grobs = lapply(
plot_list,
set_panel_size,
width = unit(width, "cm"),
height = unit(height, "cm")
),nrow=nrow) -> p
p
}