I want to plot a karyogram with SNP markers.
It works with function segments
but I want to use ggplot2 package to display an elegant graphic.
- ggbio:
I checked the package ggbio with the functionlayout_karyogram
but the chromosomes are plotted in a vertical position. I didn't find a way to rotate the graph with the name below each chromosome and to write the name of my SNP next to their segment.
geom_bar
:
Then I triedgeom_bar
from the package ggplot2:
data<-data.frame(chromosome=paste0("chr", 1:4),size=c(100,400,300,200),stringsAsFactors = FALSE) dat$chromosome<-factor(dat$chromosome, levels = dat$chromosome) SNP<-data.frame(chromosome=c(1,1,2,3,3,4),Position=c(50,70,250,20,290,110),Type=c("A","A","A","B","B","B"),labels=c("SNP1","SNP2","SNP3","SNP4","SNP5","SNP6")) p <- ggplot(data=data, aes(x=chromosome, y=size)) + geom_bar( stat="identity", fill="grey70",width = .5) +theme_bw() p + geom_segment(data=SNP, aes(x=SNP$chromosome-0.2, xend=SNP$chromosome+0.2, y=SNP$Position,yend=SNP$Position,colour=SNP$Type), size=1) +annotate("text", label =SNP$labels, x =SNP$chromosome-0.5, y = SNP$Position, size = 2, colour= "red")
The only problem here, it looks more like a barplot than a chromosome. I would like to have rounded extremities. I found someone who got the same problem as I am.
geom_path
:
Instead of usinggeom_bar
, I usedgeom_path
with the optionlineend = "round"
to get rounded extremities.
ggplot() + geom_path(data=NULL, mapping=aes(x=c(1,1), y=c(1,100)),size=3, lineend="round")
The shape looks quite good. So I tried to run the code for severals chromosomes.
p <- ggplot() data<-data.frame(chromosome=paste0("chr", 1:4),size=c(100,400,300,200),stringsAsFactors = FALSE) for (i in 1:length(data[,1])){ p <- p + geom_path(data=NULL, mapping=aes(x=c(i,i), y=c(1,data[i,2])), size=3, lineend="round") }
It doesn't work, I don't know why but p only save the last chromosome instead of plotting the four chromosomes in my karyogram.
Any suggestions for these problems ?