-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_grid_plot.R
executable file
·538 lines (447 loc) · 22 KB
/
path_grid_plot.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# utility function to cut a string to the desired number of characters
shorten <- function(string) {
#substr(string, 1, 0)
string
}
#splits a matrix of pathways X treatments based on the provided hierarchy and the deired level of separation
#return a list of (matrix, subHierarchy) one element for each distinct level
paths_bylev <- function (kegg_hierarchy,kegg_mat_cell,split_level=1) {
levels_mat <- list()
#select pathways that are present in the matrix
hierarchy_sub <- kegg_hierarchy[kegg_hierarchy$Pathway %in% colnames(kegg_mat_cell),]
#order the matrix columns following the provided hyerarchy
mat_cell_ord <- kegg_mat_cell[,as.character(hierarchy_sub$Pathway)]
#split the input matrix using pathways groups defined by the provided split_level in the hierarchy
for(level in unique(hierarchy_sub[,split_level])){
#take sub-hierarchy rooted in level
lev_hierarchy <- hierarchy_sub[hierarchy_sub[,split_level]==level,]
#extract columns with pathways in the defined sub-hierarchy
kegg_lev <- mat_cell_ord[,colnames(mat_cell_ord) %in% lev_hierarchy$Pathway]
if (sum(colnames(mat_zcell_ord) %in% lev_hierarchy$Pathway)==1){
kegg_lev <- as.matrix(kegg_lev)
colnames(kegg_lev) <- lev_hierarchy$Pathway
}
#create a nemed list element containing sub matrix and sub pathway
levels_mat[[length(levels_mat)+1]] <- list(kegg_lev,lev_hierarchy)
names(levels_mat)[length(levels_mat)] <- level
}
return(levels_mat)
}
update_hierarchy=function(kegg_hierarchy,lev1_content,lev2_content,lev3_content){
# if(is.na(lev1_content) == F) print("selected lev1 ", lev1_content ,"\n")
# if(is.na(lev2_content) == F) print("selected lev2 ", lev2_content ,"\n")
# if(is.na(lev3_content) == F) print("selected lev3 ", lev3_content ,"\n")
print("I'm updating the hierarchy...")
if("All" %in% lev1_content){
print("All in lev1")
idx1 = 1:nrow(kegg_hierarchy)
}else{
idx1= which(kegg_hierarchy[,1] %in% lev1_content)
}
if("All" %in% lev2_content){
print("All in lev2")
idx2 = 1:nrow(kegg_hierarchy)
}else{
idx2= which(kegg_hierarchy[,2] %in% lev2_content)
}
if("All" %in% lev3_content){
print("All in lev3")
idx3 = 1:nrow(kegg_hierarchy)
}else{
idx3= which(kegg_hierarchy[,3] %in% lev3_content)
}
idx = intersect(idx1,intersect(idx2,idx3))
#print("Selected idx: ")
#print(idx)
return(kegg_hierarchy[idx,])
}
#collapse a matrix of pathways X treatments based on the provided hierarchy and the deired level
#return a list of (matrix, subHierarchy) one element for each distinct collapsing level
# the value is computed summarising row elements with the provided function col_fun (default is median)
collapse_paths <- function (kegg_hierarchy,kegg_mat_cell, collapse_level=1,col_fun=function(x){median(x,na.rm = T)}) {
#save(kegg_hierarchy,kegg_mat_cell,collapse_level,col_fun, file="demo/collapse_path.RData")
#select pathways that are present in the matrix
hierarchy_sub <- kegg_hierarchy[kegg_hierarchy$ID %in% colnames(kegg_mat_cell),]
#order the matrix columns following the provided hyerarchy
mat_cell_ord <- kegg_mat_cell[,as.character(hierarchy_sub$ID)]
#get the list of categories on which we will collapse our pathways
#keg_lev <- unique(hierarchy_sub[,collapse_level])
if(collapse_level==1){
keg_lev <- unique(hierarchy_sub[,collapse_level])
}else{
if(collapse_level==3){
keg_lev <- hierarchy_sub[,collapse_level]
}
else{
keg_lev <- unique(hierarchy_sub[,1:collapse_level])
keg_lev = keg_lev[,2]
}
}
#create a new matrix with columns corresponding to collapse categories
if(is.null(nrow(mat_cell_ord))){
mat_cell_ord = matrix(mat_cell_ord,ncol=1,nrow=length(mat_cell_ord),dimnames = list(as.character(names(mat_cell_ord)),hierarchy_sub$ID))
}
lev_mat <- matrix(NA,ncol = length(keg_lev),nrow = nrow(mat_cell_ord),dimnames = list(rownames(mat_cell_ord),keg_lev))
#for each category take all pathways in that category and summaryze their values for each row (sample)
for (lev in keg_lev){ #controllare che il padre e' arricchito. se e' arricchito ci metti il padre (ci metti asterisco), altrimenti fai il summary
#take sub-hierarchy rooted in lev
path_lev <- unique(hierarchy_sub[hierarchy_sub[,collapse_level] == lev, ]$ID)
#extract columns with pathways in the defined sub-hierarchy
mat_cell_sub <- as.matrix(mat_cell_ord[,colnames(mat_cell_ord) %in% path_lev])
#summarize vaules over rows using the provided col_fun function
summary <- unlist(apply(mat_cell_sub,1,FUN = col_fun))
#store summarized column for culumn lev
## if duplicate column names in mat it was assigning only the first
# lev_mat[,lev] <- summary
lev_mat[,colnames(lev_mat) %in% lev] <- summary
}
#create a nemed list element containing collapsed matrix and collapsed hierarchy as well
lev_hierarchy <- unique(as.data.frame(hierarchy_sub[,1:collapse_level]))
colnames(lev_hierarchy) <- colnames(hierarchy_sub)[1:collapse_level]
return(list(lev_mat,lev_hierarchy))
}
#plots a matrix having colums for wich a hierachy is provided
# path_mat is matrix to be plotted
# path_hier is the hierarchy defined over columns
# experiment_ann is a vector of the same length as # of rows of path_mat defining grouping for samples
# discrete tells the function if the value are to be plotted using a countinuos scale or a discrete scale
# square_colors if a discrete scale is chosen, than the colours for each possible value have to be provided
# color_leg if a discrete scale is chosen, than the colour legend for each possible value has to be provided
# level_col level (column number) from the hierarchy used to group columns (pathways)
plot_grid <- function(path_mat,path_hier, title="", experiment_ann=c(),discrete=F,square_colors=c(),color_leg=c(),level_col=1,treat_text_size=8,path_text_size=6, asRatio = FALSE) {
#save(path_mat, path_hier,experiment_ann,title,discrete,square_colors,color_leg,
# level_col,treat_text_size,path_text_size,file="demo/demo_plot.RData")
#path_mat = path_mat[rownames(experiment_ann),]
#define the groups from the hierarchy and the chosen level
path_col <- factor(path_hier[,level_col], levels = unique(path_hier[,level_col]))
#prepare a set of colors to assign to each group
#darkcols <- c(brewer.pal(8, "Dark2"),brewer.pal(8, "Accent")[-4],brewer.pal(12, "Paired")[-11])
#using ggplot2 we need to melt the input matrix
kegg_melt <- melt(path_mat)
colnames(kegg_melt)[1:2] = c("Var1","Var2")
#if discrete create value-color mapping using input values or this default scale
if (discrete){
if(length(square_colors)==0){
colors <- c("-1"="darkgreen","0"="white","1"="red")
color_leg <- c("-1"= "negative","0"="neutral","1"="positive")
}else {colors <- square_colors}
#if discrete we need to convert values in the matrix in factors to esure ggplot will plot on a discrete scale
kegg_melt$value <- as.factor(kegg_melt$value)
}
# if a grouping for the sample is provided assign it to the melted dataframe rows (ncol by ncol)
#this will be used in faceting
if(length(experiment_ann[,1])>0){
#print(path_mat)
#print(kegg_melt)
#print(experiment_ann)
#print(ncol(path_mat))
x = experiment_ann[which(experiment_ann[,2] %in% rownames(path_mat)),1]
#print(x)
kegg_melt$experiment <- rep(x,ncol(path_mat))
}else{
#otherwise assign a default group to all samples
kegg_melt$experiment <- as.factor("treatment")
}
#assign group fromf rom the hierarchy and the chosen level (path_col) to the melted dataframe rows (nrow by nrow)
#this will be used in faceting
kegg_melt$path_group <- factor(as.character(rep(path_col,each=nrow(path_mat))),levels=unique(as.character(rep(path_col,each=nrow(path_mat)))))
#prepare a set of colors to assign to each group
darkcols <- randomColor(length(unique(kegg_melt$path_group)),luminosity = "dark")
# prepare the ggplot instance -- to render groups we will use faceting
#using tile plot to render thje matrix as a grid of squares
#print(kegg_melt$experiment)
print(head(kegg_melt))
ukm = unique(kegg_melt$experiment)
print(ukm)
for(i in 1:nrow(kegg_melt)){
kegg_melt[i,"experiment"] = which(ukm %in% kegg_melt[i,"experiment"] )
}
print(head(kegg_melt))
#Ordinamento per mantenere ordinamento i gruppi ordinati secondo ordine numerico
labelx = sort(unique(as.numeric(as.character(kegg_melt$experiment))))
print(labelx)
kegg_melt$experiment = factor(kegg_melt$experiment,levels = labelx)
#Ordiniamo i campioni con lo stesso ordine delle foglie dell'albero gerarchico
kegg_melt$Var1 = factor(kegg_melt$Var1,levels = unique(kegg_melt$Var1))
if(discrete == FALSE){
#the values in kegg_melt must be numeric, not factors
kegg_melt$value = as.numeric(as.vector(kegg_melt$value))
}
p <- ggplot(kegg_melt, aes(Var1, Var2, fill = value)) +
facet_grid(path_group~experiment,scales="free",space="free",labeller = labeller(path_group=shorten)) +
# theme(axis.text.x = element_text(colour = as.numeric(as.factor(experiment_ann))),
# axis.text.y = element_text(colour = darkcols[as.numeric(path_col)])) +
geom_tile(colour = "black",size=0.1) +
ggtitle(title) +
theme(plot.title = element_text(hjust = 0.5)) +
#coord_equal() +
theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=0, size=treat_text_size),
axis.text.y = element_text(size=path_text_size),
#plot.margin = margin(1, 1, 1, 1, "cm"),
strip.text.y = element_text(angle = 0,size = 9,face = "bold")) +
scale_x_discrete(position = "top") +
labs(x = "", y="")
if(asRatio) p = p+theme(aspect.ratio = 1)
if (discrete){
p <- p + scale_fill_manual(values=colors,labels=color_leg,na.value = 'gray50')
}else{
#limits=c(min(kegg_melt$value[is.na(kegg_melt$value)==FALSE]), max(kegg_melt$value[is.na(kegg_melt$value)==FALSE]))
p <- p + scale_fill_gradient(low = "darkgreen", high = "red",na.value = 'gray50')
}
#afrer faceting we need to open the ggplot object to put different row/column label colors to different facets
png(file.path(tempdir(),"veer.png"))
gplot <- ggplotGrob( p )
dev.off()
nms <- lapply( gplot$grobs , function(x) names( x[]$children ) )
#we search for axis.line.x and axis.line.y containers
grbs_x_id <- which( sapply( lapply( nms , function(x) grepl( "axis.line.x" , x ) ) , any ) == 1 )
grbs_y_id <- which( sapply( lapply( nms , function(x) grepl( "axis.line.y" , x ) ) , any ) == 1 )
#grob variable iterates over blocks of colums of the ggplot
#for (grob in (1:length(grbs_x_id))){
# gplot$grobs[[grbs_x_id[grob]]]$children$axis$grobs[[1]]$children[[1]]$gp$col=grob
#}
#grob variable iterates over blocks of rows of the ggplot
#for (grob in (1:length(grbs_y_id))){
# gplot$grobs[[grbs_y_id[grob]]]$children$axis$grobs[[1]]$children[[1]]$gp$col=darkcols[grob]
#}
#grob variable iterates over blocks of colums of the ggplot
for (grob in (1:length(grbs_x_id))){
to_select <- grbs_x_id[grob]
if (length(to_select) >= 1) { # Check added because there was a 0 length vector. Luca
item_at_grob <- gplot$grobs[[to_select]]
if (! is.null(item_at_grob)) { # Check added because there where nulls. Luca
item_at_grob$children$axis$grobs[[1]]$children[[1]]$gp$col=grob
}
}
}
#grob variable iterates over blocks of rows of the ggplot
for (grob in (1:length(grbs_y_id))){
to_select <- grbs_y_id[grob]
if (length(to_select) >= 1) {
item_at_grob <- gplot$grobs[[to_select]]
if (! is.null(item_at_grob)) {
item_at_grob$children$axis$grobs[[1]]$children[[1]]$gp$col=darkcols[grob]
}
}
}
# save(list = ls(all.names = TRUE),file = "ggplotting")
#plot the final result
#plot(gplot) #Commented by Veer
return(gplot)
}
#plots all 3 collapsed leayers of a kegg matrix and the sub matrices obtained by splitting at level 1
plot_kegg_mat <- function(kegg_hierarchy, kegg_mat_cell, group_col,discrete=T, pre_title="",square_colors=c(),color_leg=c(),path_text_size=12,treat_text_size=12,lev_3_cex_row=10,lev_3_cex_col=10) {
#collapse at level 1 using the median to summarize vaulues
kegg_nano_1 <- collapse_paths(kegg_hierarchy = kegg_hierarchy,kegg_mat_cell = kegg_mat_cell, collapse_level = 1)
#extract collapsed matrix and collapsed hierarachy
mat <- kegg_nano_1[[1]]
hier <- kegg_nano_1[[2]]
#plot the collapsed matrix
plot_grid(path_mat = mat,path_hier = hier,experiment_ann = group_col,discrete = discrete,level_col = 1,title = paste(pre_title,"level1"),square_colors,color_leg,path_text_size = path_text_size,treat_text_size = treat_text_size)
#collapse at level 1 using the median to summarize vaulues
kegg_nano_2 <- collapse_paths(kegg_hierarchy = kegg_hierarchy,kegg_mat_cell = kegg_mat_cell,collapse_level = 2)
mat <- kegg_nano_2[[1]]
hier <- kegg_nano_2[[2]]
plot_grid(path_mat = mat,path_hier = hier,experiment_ann = group_col,discrete = discrete,level_col = 1,title = paste(pre_title,"level2"),square_colors,color_leg,path_text_size = path_text_size,treat_text_size = treat_text_size)
#collapse at level 3 using the median to summarize vaulues
#this call is on the last levele hence no summarizatrion is done
#we exploit the side effect of reordering and filtering of the hierarchy
kegg_nano_3 <- collapse_paths(kegg_hierarchy = kegg_hierarchy,kegg_mat_cell = kegg_mat_cell, collapse_level = 3)
mat <- kegg_nano_3[[1]]
hier <- kegg_nano_3[[2]]
plot_grid(path_mat = mat,path_hier = hier,experiment_ann = group_col, discrete = discrete,level_col = 1, title = paste(pre_title,"level3"),square_colors,color_leg,path_text_size = lev_3_cex_row,treat_text_size = lev_3_cex_col)
#split the matrix with levels defined by level 1
path_by_lev_list <- paths_bylev(kegg_hierarchy = kegg_hierarchy, kegg_mat_cell = kegg_mat_cell, split_level = 1)
# plot sub matrices one at time grouping and coloring at level 2
for (i in 1:length(path_by_lev_list)){
mat <- path_by_lev_list[[i]][[1]]
hier <- path_by_lev_list[[i]][[2]]
plot_grid(path_mat = mat,path_hier = hier,experiment_ann = group_col,discrete = discrete,level_col = 2,title = paste(pre_title,names(path_by_lev_list)[i]),square_colors,color_leg,path_text_size = path_text_size,treat_text_size = treat_text_size)
}
}
#Plot grid map for genes of a selected pathway
plot_grid_genes <- function(path_mat, title="", experiment_ann=c(), gene_group=NULL, discrete=F, square_colors=c(), color_leg=c(), treat_text_size=8, asRatio=TRUE){
kegg_melt <- melt(path_mat)
colnames(kegg_melt)[1:2] = c("Var1","Var2")
#if discrete create value-color mapping using input values or this default scale
if (discrete){
if(length(square_colors)==0){
colors <- c("-1"="darkgreen","0"="white","1"="red")
color_leg <- c("-1"= "negative","0"="neutral","1"="positive")
}else{
colors <- square_colors
}
#if discrete we need to convert values in the matrix in factors to esure ggplot will plot on a discrete scale
kegg_melt$value <- as.factor(kegg_melt$value)
}
# if a grouping for the sample is provided assign it to the melted dataframe rows (ncol by ncol)
#this will be used in faceting
print("experiment_ann")
print(experiment_ann)
if(length(experiment_ann[,1])>0){
print("Experiment is present!")
#print(path_mat)
#print(kegg_melt)
#print(experiment_ann)
#print(ncol(path_mat))
x = experiment_ann[which(experiment_ann[,2] %in% rownames(path_mat)),1]
#print(x)
print("dim(kegg_melt)")
print(dim(kegg_melt))
print("str(kegg_melt)")
print(str(kegg_melt))
print("str(rep(x,ncol(path_mat)))")
print(str(rep(x,ncol(path_mat))))
kegg_melt$experiment <- rep(x,ncol(path_mat))
}else{
print("Creating fake 'experiment'!")
#otherwise assign a default group to all samples
kegg_melt$experiment <- as.factor("treatment")
}
#using tile plot to render thje matrix as a grid of squares
#print(kegg_melt$experiment)
print("head(kegg_melt)")
print(head(kegg_melt))
ukm = unique(kegg_melt$experiment)
print("Unique experiments:")
print(ukm)
for(i in 1:nrow(kegg_melt)){
kegg_melt[i,"experiment"] = which(ukm %in% kegg_melt[i,"experiment"])
}
print("head(kegg_melt)")
print(head(kegg_melt))
#Ordinamento per mantenere ordinamento i gruppi ordinati secondo ordine numerico
labelx = sort(unique(as.numeric(as.character(kegg_melt$experiment))))
print("labelx")
print(labelx)
kegg_melt$experiment = factor(kegg_melt$experiment, levels=labelx)
#Ordiniamo i campioni con lo stesso ordine delle foglie dell'albero gerarchico
kegg_melt$Var1 = factor(kegg_melt$Var1, levels=unique(kegg_melt$Var1))
if(discrete == FALSE){
#the values in kegg_melt must be numeric, not factors
kegg_melt$value = as.numeric(as.vector(kegg_melt$value))
}
if(!is.null(gene_group)){
print("class(gene_group)")
print(class(gene_group))
print("str(gene_group)")
print(str(gene_group))
# print("gene_group")
# print(gene_group)
kegg_melt$gene_group = gene_group
}
#Make pathway melt
kegg_melt_path <- dplyr::filter(kegg_melt, gene_group=="Pathway")
print("str(kegg_melt_path)")
print(str(kegg_melt_path))
#Pathway map
pPath <- ggplot(kegg_melt_path, aes(Var1, Var2, fill=value)) +
facet_grid(.~experiment, scales="free", space="free") +
geom_tile(colour="black", size=0.1) +
ggtitle(title) +
theme(plot.title=element_text(hjust=0.5)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=0, size=treat_text_size),
axis.text.y = element_text(size=treat_text_size, color="black"),
strip.text.y=element_text(angle=0, size=9, face="bold")) +
scale_x_discrete(position="top") +
labs(x="", y="")
if(asRatio) pPath = pPath+theme(aspect.ratio = 1)
if (discrete){
pPath <- pPath + scale_fill_manual(values=colors,labels=color_leg,na.value = 'gray50')
}else{
pPath <- pPath + scale_fill_gradient(low = "darkgreen", high = "red",na.value = 'gray50')
}
#afrer faceting we need to open the ggplot object to put different row/column label colors to different facets
png(file.path(tempdir(),"juen.png"))
gplotPath <- ggplotGrob( pPath )
dev.off()
nms <- lapply( gplotPath$grobs , function(x) names( x[]$children ) )
#we search for axis.line.x and axis.line.y containers
grbs_x_id <- which( sapply( lapply( nms , function(x) grepl( "axis.line.x" , x ) ) , any ) == 1 )
grbs_y_id <- which( sapply( lapply( nms , function(x) grepl( "axis.line.y" , x ) ) , any ) == 1 )
#grob variable iterates over blocks of colums of the ggplot
# for (grob in (1:length(grbs_x_id))){
# gplotPath$grobs[[grbs_x_id[grob]]]$children$axis$grobs[[1]]$children[[1]]$gp$col=grob
# }
for (grob in (1:length(grbs_x_id))){
to_select <- grbs_x_id[grob]
if (length(to_select) >= 1) {
item_at_grob <- gplotPath$grobs[[to_select]]
if (! is.null(item_at_grob)) {
item_at_grob$children$axis$grobs[[1]]$children[[1]]$gp$col=grob
}
}
}
#Make genes melt
kegg_melt_gene <- dplyr::filter(kegg_melt, gene_group=="Genes")
print("str(kegg_melt_gene)")
print(str(kegg_melt_gene))
##Genes map
p <- ggplot(kegg_melt_gene, aes(Var1, Var2, fill=value)) +
facet_grid(.~experiment, scales="free", space="free") +
geom_tile(colour="black", size=0.1) +
ggtitle(title) +
theme(plot.title=element_text(hjust=0.5)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=0, size=treat_text_size),
axis.text.y = element_text(size=treat_text_size, color="black"),
strip.text.y=element_text(angle=0, size=9, face="bold")) +
scale_x_discrete(position="top") +
labs(x="", y="")
### This commented plot was used for faceted pathway and genes map ###
# p <- ggplot(kegg_melt, aes(Var1, Var2, fill=value))
#
# if(is.null(gene_group)){
# p <- p + facet_grid(.~experiment, scales="free", space="free")
# }else{
# p <- p + facet_grid(gene_group~experiment, scales="free", space="free")
# }
#
# p <- p +
# geom_tile(colour="black", size=0.1) +
# ggtitle(title) +
# theme(plot.title=element_text(hjust=0.5)) +
# theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=0, size=treat_text_size),
# axis.text.y = element_text(size=treat_text_size, color="black"),
# strip.text.y=element_text(angle=0, size=9, face="bold")) +
# scale_x_discrete(position="top") +
# labs(x="", y="")
if(asRatio) p = p+theme(aspect.ratio = 1)
if (discrete){
p <- p + scale_fill_manual(values=colors,labels=color_leg,na.value = 'gray50')
}else{
p <- p + scale_fill_gradient(low = "darkgreen", high = "red",na.value = 'gray50')
}
#afrer faceting we need to open the ggplot object to put different row/column label colors to different facets
png(file.path(tempdir(),"juen.png"))
gplot <- ggplotGrob( p )
dev.off()
nms <- lapply( gplot$grobs , function(x) names( x[]$children ) )
#we search for axis.line.x and axis.line.y containers
grbs_x_id <- which( sapply( lapply( nms , function(x) grepl( "axis.line.x" , x ) ) , any ) == 1 )
grbs_y_id <- which( sapply( lapply( nms , function(x) grepl( "axis.line.y" , x ) ) , any ) == 1 )
#grob variable iterates over blocks of colums of the ggplot
# for (grob in (1:length(grbs_x_id))){
# gplot$grobs[[grbs_x_id[grob]]]$children$axis$grobs[[1]]$children[[1]]$gp$col=grob
# }
for (grob in (1:length(grbs_x_id))){
to_select <- grbs_x_id[grob]
if (length(to_select) >= 1) {
item_at_grob <- gplot$grobs[[to_select]]
if (! is.null(item_at_grob)) {
item_at_grob$children$axis$grobs[[1]]$children[[1]]$gp$col=grob
}
}
}
# #grob variable iterates over blocks of rows of the ggplot
# for (grob in (1:length(grbs_y_id))){
# gplot$grobs[[grbs_y_id[grob]]]$children$axis$grobs[[1]]$children[[1]]$gp$col=darkcols[grob]
# }
#return(p)
#return(gplot)
#Combine ggplot for pathway and genes
#gplotComb <- arrangeGrob(gplotPath, gplot, nrow=2, ncol=1, widths=1)
gplotComb <- rbind(gplotPath, gplot, size="first")
print("str(gplotComb)")
print(str(gplotComb))
return(gplotComb)
}