Question: Need help with R script to edit x-axis of snp density plot
1
gravatar for deepti1rao
15 months ago by
deepti1rao30
deepti1rao30 wrote:

I am using the following code to plot Chromosome-wide SNP densities. It generates the attached output, such that X-axis for all the chromosomes is of the same length. It can be misleading, as it appears as if the right ends of some (reads short) chromosomes is totally devoid of snps. Can I have shorter X-axes for the shorter chromosomes?? Please help me edit the script to suit my needs

SNP density plot

snps<-read.table("no_C_no_M_homo_snps.Galaxy12-[Cut_on_data_10].tabular",sep="\t",header=F,blank.lines.skip=TRUE,
                 comment.char = "#")
colnames(snps)<-c("chr","start","id","refallele","altallele","qual",
                  +                   "filter","info","format")
summary(snps)
goodChrOrder <- c(paste("Chr",c(1:12),sep=""))
snps$chr <- factor(snps$chr,levels=goodChrOrder)

Plot the densities of snps in the bed file for each chr seperately

library(ggplot2)
snpDensity<-ggplot(snps) + 
geom_histogram(aes(x=start),binwidth=1e4,color="brown4") + # pick a binwidth that is not too small 
    +     facet_wrap(~ chr,ncol=2) + # seperate plots for each chr, x-scales can differ from chr to chr
    +     ggtitle("Chromosome-wise homozygous snp distribution") + theme(plot.title = element_text(hjust = 0.5)) +
xlab("Genomic location (bp)") + 
ylab("SNP density")  
p <- snpDensity
p.labs <- p + labs(title = "Chromosome-wise homozygous SNP distribution", x = "Genomic location(bp)", y = "SNP density")
p.labs
y.6.text <- element_text(size = 6)
## for y axis only
p.labs + theme(axis.text.y = y.6.text)
chromosome density snps R • 1.2k views
ADD COMMENTlink modified 13 months ago by bernatgel2.2k • written 15 months ago by deepti1rao30
1

Free scale the axis @op

ADD REPLYlink written 15 months ago by cpad011212k
1

"scales: Are scales shared across all facets (the default, "fixed"), or do they vary across rows ("free_x"), columns ("free_y"), or both rows and columns ("free")"

So try adding a scales="free_x" argument to your facet_wrap function.

ADD REPLYlink written 15 months ago by Russ460

some example data would help @OP

ADD REPLYlink written 15 months ago by cpad011212k

Can anyone help with this?

ADD REPLYlink written 15 months ago by deepti1rao30

You may wish to reconsider your plot type, as you cannot use a facet when your common axis (chr position) is dissimilar in scale This is possible. See: C: Need help with R script to edit x-axis of snp density plot

I'd also recommend you try generating separate plots + arranging them into a grid using gridExtra::arrangeGrob or the cowplot package.

ADD REPLYlink modified 15 months ago • written 15 months ago by RamRS24k

I have same problem here. Did you figure out how to get shorter x-axis for smaller chromosomes? Thanks!

ADD REPLYlink written 10 months ago by xia0100
1
gravatar for bernatgel
13 months ago by
bernatgel2.2k
Barcelona, Spain
bernatgel2.2k wrote:

I would recommend using a tool specific for plotting data on the genome such as karyoploteR. With this kind of tools, you'll get all your chromosomes into scale for free.

The code would be something like this (untested):

library(karyoploteR)

snps<-read.table("no_C_no_M_homo_snps.Galaxy12-[Cut_on_data_10].tabular",sep="\t",header=F,blank.lines.skip=TRUE,
             comment.char = "#")
colnames(snps)<-c("chr","start","id","refallele","altallele","qual", "filter","info","format")

#make a GRanges with your data (we need to repeat column 2 as start and end for this to work)
snps.gr <- toGRanges(snps[,c(1,2,2)])

#Begin plotting
kp <- plotKaryotype(genome="hg19") #<- use the genome you need, if not human
kp <- kpPlotDensity(kp, data=snps.gr, col="blue")

#To add the axis, first get the max density value
max.density <- kp$latest.plot$computed.values$max.density
kpAxis(kp, ymin=0, ymax=max.density, numticks = 3)

And that's it. With this you should get something like this

enter image description here

You can customize it in many ways (change colors, etc...) or represent only some of the chromosomes or even zoom to specific regions.

You can find more info on that in the karyoploteR tutorial page and specifically in the kpPlotDensity page or the Gene Density example.

Hope this helps

ADD COMMENTlink written 13 months ago by bernatgel2.2k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1881 users visited in the last hour