Karyogram和SNP与ggplot2(geom_path,geom_bar,ggbio)

时间:2017-03-28 09:27:23

标签: r ggplot2 geom-bar

我想用SNP标记绘制核型图 它适用于函数segments,但我想使用ggplot2包来显示优雅的图形 enter image description here

  1. ggbio:
    我用函数layout_karyogram检查了包ggbio,但是染色体是在垂直位置绘制的。我没有找到一种方法来旋转图形,每个染色体下面都有一个名称,并在其区段旁边写下我的SNP的名称。
  2. enter image description here

    1. geom_bar
      然后我尝试了ggplot2包中的geom_bar
    2. 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")    
      

      enter image description here

      这里唯一的问题是,它看起来更像是一个条纹而不是染色体。我想要四肢四肢。 I found someone who got the same problem as I am

      1. geom_path
        我没有使用geom_bar,而是使用geom_path选项lineend = "round"来获得四舍五入。
      2. ggplot() + geom_path(data=NULL, mapping=aes(x=c(1,1), y=c(1,100)),size=3, lineend="round")   
        

        enter image description here

        形状看起来很不错。所以我试着为几条染色体运行代码。

        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")
        }   
        

        enter image description here

        它不起作用,我不知道为什么,但p只能保存最后一条染色体而不是在我的核型图中绘制四条染色体。

        对这些问题的任何建议?

1 个答案:

答案 0 :(得分:4)

我会选择geom_segment。 SNP段的x开始/结束是硬编码的(as.integer(chr) -+ 0.05),否则代码非常简单。

ggplot() +
  geom_segment(data = data,
               aes(x = chr, xend = chr, y = 0, yend = size),
               lineend = "round", color = "lightgrey", size = 5) +
  geom_segment(data = SNP,
               aes(x = as.integer(chr) - 0.05, xend = as.integer(chr) + 0.05,
                   y = pos, yend = pos, color = type),
               size = 1) +
  theme_minimal()

enter image description here

data <- data.frame(chr = paste0("chr", 1:4),
                   size = c(100, 400, 300, 200))

SNP <- data.frame(chr = paste0("chr", c(1, 1, 2, 3, 3, 4)),
                  pos = c(50, 70, 250, 20, 290, 110),
                  type = c("A", "A", "A", "B", "B", "B"))