Install ggplot2 with install.packages before importing for plotting purposes below.
#install.package("ggplot2")
library(ggplot2)
path = '/home/genewalk/qki/'
filename = 'genewalk_results.csv'
GW = read.csv(paste(path,filename,sep=""),header = TRUE)
GW$mlog10padj = -log10(GW$gene_padj) #calculate -log10 values
GW$mlog10padj_err = -log10(GW$cilow_gene_padj) - GW$mlog10padj #calculate -log10 error bars
alpha_FDR_plot = 0.05
GENES = c('MAL','PLP1')
for (gene in GENES){
dplot = GW[which(GW$hgnc_symbol==gene),]
#for plotting: order GO terms according to gene_padj (and if equal then sort by cosine similarity)
dplot$go_name = factor(dplot$go_name,
levels = dplot$go_name[with(dplot,order(-gene_padj,sim))])
font_sz=12
ymax=max(dplot$mlog10padj + dplot$mlog10padj_err) + 0.3
f = ggplot(dplot, aes(go_name,mlog10padj)) +
theme_light(base_size=font_sz) +
geom_bar(stat='identity',fill='royalblue',width=0.8) +
geom_errorbar(aes(ymin=mlog10padj-mlog10padj_err, ymax=mlog10padj+mlog10padj_err),
width=0.2, color='grey') +
geom_hline(yintercept=-log10(alpha_FDR_plot), color='red', linetype='dashed') +
xlab('') +
ylab('-log10(gene p-adjust)') +
theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_text(size=font_sz*0.8)) +
scale_y_continuous(breaks=c(0,1,2,3,4),
limits = c(0, ymax)) +
coord_flip()+
ggtitle(gene)
#f
gonames = as.character(dplot$go_name)
lchar = sapply(gonames, nchar)
w0 = max(lchar)
fname = paste('barplot_',gene,'_x_mlog10gene_padj_y_GO.',sep="")
ggsave(paste(path,fname,'pdf',sep=""),
f,
width = (ymax*0.3+w0*0.1),
height=(0.2+0.25*length(dplot$go_name)))#units:inch
ggsave(paste(path,fname,'png',sep=""),
f,
width = (ymax*0.3+w0*0.1),
height=(0.2+0.25*length(dplot$go_name)))
}
sessionInfo()
## R version 3.5.0 (2018-04-23)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggplot2_3.3.2
##
## loaded via a namespace (and not attached):
## [1] knitr_1.30 magrittr_1.5 tidyselect_1.1.0 munsell_0.5.0
## [5] colorspace_1.4-1 R6_2.4.1 rlang_0.4.8 stringr_1.4.0
## [9] dplyr_1.0.2 tools_3.5.0 grid_3.5.0 gtable_0.3.0
## [13] xfun_0.18 withr_2.3.0 htmltools_0.5.0 ellipsis_0.3.1
## [17] yaml_2.2.1 digest_0.6.26 tibble_3.0.4 lifecycle_0.2.0
## [21] crayon_1.3.4 farver_2.0.3 purrr_0.3.4 vctrs_0.3.4
## [25] glue_1.4.2 evaluate_0.14 rmarkdown_2.5 stringi_1.5.3
## [29] compiler_3.5.0 pillar_1.4.6 generics_0.0.2 scales_1.1.1
## [33] pkgconfig_2.0.3