#column1 = chromosome name and column2 = start position of the gene
# check if ggplot2 is installed, if so, load it,
# if not, install and load it
if("ggplot2" %in% rownames(installed.packages())){
library(ggplot2)
} else {
install.packages("ggplot2")
library(ggplot2)
}
# import a text file with gene positions
# columns should be: chr, position (no end or gene name required)
genes <- read.table("genes.txt",sep="\t",header=T)
# make sure the chromosomes are ordered in the way you want
# them to appear in the plot
genes$chr <- with(genes, factor(chr, levels=paste("chr",c(1:22,"X","Y"),sep=""), ordered=TRUE))
# make a density plot of genes over the provided chromosomes (or scaffolds ...)
plottedGenes <- ggplot(genes) + geom_histogram(aes(x=pos),binwidth=1000000) + facet_wrap(~chr,ncol=2) + ggtitle("RefSeq genes density over human genome 19") + xlab("Genomic position (bins 1 Mb)") + ylab("Number of genes")
# save it to an image
png("genes.png",width=1000,height=1500)
print(plottedGenes)
dev.off()