代码之家  ›  专栏  ›  技术社区  ›  user3224522

绘制不同ymin和ymax的重叠几何图形

  •  1
  • user3224522  · 技术社区  · 6 年前

    我想把下面的数据绘制成 geom_rect 使用 ggplot2 R . 有可能画出一些相互重叠的图吗?对我来说,策划 几何 ymin ymax . 感谢您的帮助!

    chr start stop 
    a   3   7
    a   6   8
    a   5   10
    a   12  14
    b   6   8
    b   3   8
    b   2   5
    b   2   5
    
    Genes <- ggplot(mydata, aes(xmin=start,ymin=0.20,xmax=stop,ymax=0.50),inherit.aes=FALSE) +
      xlab("position") +
      geom_rect(color="black") +
      theme(legend.position="none") +
      theme_bw() +
      theme(text=element_text(size=10)) +
      facet_wrap(~chr, ncol = 2, strip.position = "right")
    plot(Genes)
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   missuse    6 年前

    可能可以使用填充和alpha的某种组合来显示底层重叠。这是一个主意:

    ggplot(df %>%
             mutate(n = 1:n()),
           aes(xmin = start,
               ymin = as.numeric(chr) - 0.1,
               xmax = stop,
               ymax = as.numeric(chr) + 0.1)) +
      xlab("position") +
      geom_rect(color = "black", alpha = 0.5) +
      scale_y_continuous(breaks = c(1, 2),
                         labels = levels(df$chr))+
      theme(legend.position = "none",
            text = element_text(size = 10)) +
      theme_bw() 
    

    enter image description here

    区域越暗,重叠越多。如果重叠很少,这可能很方便,但如果有很多重叠,我会强调如下:

    ggplot(df %>%
             group_by(chr) %>%
             mutate(n = 1:n()),
           aes(xmin = start,
               ymin = n - 0.5,
               xmax = stop,
               ymax = n + 0.5)) +
      xlab("position") +
      geom_rect(color = "black", alpha = 0.5) +
      facet_wrap(~chr, ncol = 1, strip.position = "right")+
      theme_bw() +
      theme(legend.position = "none",
            axis.text.y = element_blank(),
            axis.ticks.y = element_blank())
    

    enter image description here

    structure(list(chr = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L), .Label = c("a", "b"), class = "factor"), start = c(3L, 6L, 
    5L, 12L, 6L, 3L, 2L, 2L), stop = c(7L, 8L, 10L, 14L, 8L, 8L, 
    5L, 5L)), .Names = c("chr", "start", "stop"), class = "data.frame", row.names = c(NA, 
    -8L))