/
SOURCE.R
117 lines (104 loc) · 3.36 KB
/
SOURCE.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
109
110
111
112
113
114
115
116
# ----------
# General Functions
# ----------
smart_date = function(){
paste(strsplit(date()," ")[[1]][c(2,3,5)],collapse="-")
}
smart_hist = function(x,main="",...){
hist(x,col="gray",main=main,freq=FALSE,...)
lines(density(x,na.rm=TRUE),lwd=1.5,lty=2,col="blue")
}
smart_merge = function(x,y,mess=NULL,...){
if( !is.null(mess) ){
intersect_vars = paste(intersect(names(x),names(y)),collapse=", ")
cat(paste0("Merging dataframes on variables = { ",intersect_vars," }\n"))
}
merge(x,y,by=intersect(names(x),names(y)),...)
}
smart_table = function(...){
table(...,useNA='ifany')
}
smart_RT = function(...){
read.table(...,stringsAsFactors=FALSE)
}
smart_WT = function(...){
write.table(...,row.names=FALSE,quote=FALSE)
}
smart_df = function(...){
data.frame(...,stringsAsFactors=FALSE)
}
smart_sapply = function(...){
sapply(...,USE.NAMES=FALSE,simplify=FALSE)
}
smart_pack = function(pack,repo="CRAN"){
# pack = "foreach"; repo = "CRAN"
if( !pack %in% installed.packages()[,"Package"] ){
if( repo == "CRAN" ){
install.packages(pack,repos = "https://mirrors.nics.utk.edu/cran/")
} else if( repo == "BIOC" ){
source("https://bioconductor.org/biocLite.R")
biocLite(pack)
}
}
library(package = pack,character.only = TRUE)
}
name_change = function(DATA,ORIG_NAME,NEW_NAME){
index = which(names(DATA) == ORIG_NAME)
if( length(index) > 0 ){
names(DATA)[index] = NEW_NAME
}
DATA
}
gg.heatmap <- function(t2, ylab){
#create a new variable from incidence
if(max(t2$value) > 1000){
t2$valueFactor = cut(t2$value,
breaks = c(-1,0,5,10,50,100,1000,max(t2$value)),
labels=c("0","1-5","6-10","11-50","51-100","101-1000",">1000"))
}else{
t2$valueFactor = cut(t2$value,
breaks = c(-1,0,5,10,50,100,max(t2$value)),
labels=c("0","1-5","6-10","11-50","51-100","101-1000"))
}
#change level order
t2$valueFactor = factor(as.character(t2$valueFactor),
levels=rev(levels(t2$valueFactor)))
y.axis.size = 8
if(nrow(t2) > 700) y.axis.size = 7
if(nrow(t2) > 800) y.axis.size = 6
col1 = c("#f46d43","#fdae61","#fee08b","#e6f598","#abdda4","#ddf1da")
col2 = c("#d53e4f", col1)
if(max(t2$value) > 1000){
col2use = col2
}else{
col2use = col1
}
g1 = ggplot(t2, aes(Cell_Type, cluster)) +
theme_bw() +
geom_tile(aes(fill = valueFactor), color = "white", size=0.25) +
scale_fill_manual(values=col2use) +
ylab(ylab) + xlab("cell types") +
theme(legend.title=element_blank(),
legend.text=element_text(colour="grey40",size=9,face="bold"),
legend.key.height=grid::unit(0.8,"cm"),
legend.key.width=grid::unit(0.4,"cm"),
plot.title = element_text(size=16),
axis.title = element_text(size=14, face="bold"),
axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_text(size=y.axis.size),
panel.border=element_blank())
g1
}
ggplot_custom = function(DATA,X,Y,COL,TYPE){
gg = ggplot(DATA,aes_string(x = X,y = Y)) +
geom_point(size=0.2,alpha=0.6,aes_string(col = COL)) +
theme_classic() + theme(legend.position="bottom")
if( TYPE == "cont" ){
gg = gg + scale_colour_gradient(low="blue",high="red")
} else if( TYPE == "cat" ){
gg = gg + guides(color = guide_legend(
override.aes = list(size=3)))
}
gg
}
####