代码之家  ›  专栏  ›  技术社区  ›  Manasi Shah

在基R上循环以获取每个区域的PDF绘图或此绘图的ggplot替代方案

  •  2
  • Manasi Shah  · 技术社区  · 7 年前

    我正在尝试绘制以下数据帧:

        CNV_df <- structure(list(PS_position = c(1L, 405L, 705L, 1305L, 1605L, 
    1905L, 2505L, 2805L, 3105L, 3705L, 4005L, 10000L, 10400L, 10700L, 
    11400L, 11700L, 12000L, 12700L, 13300L, 13600L, 14300L, 14900L, 
    100004L, 100400L, 100700L, 101400L, 101700L, 102000L, 102400L, 
    102700L, 103000L, 103700L, 104300L, 1L, 405L, 705L, 1305L, 1605L, 
    1905L, 2505L, 2805L, 3105L, 3705L, 4005L, 10000L, 10400L, 10700L, 
    11400L, 11700L, 12000L, 12700L, 13300L, 13600L, 14300L, 14900L, 
    100004L, 100400L, 100700L, 101400L, 101700L, 102000L, 102400L, 
    102700L, 103000L, 103700L, 104300L), Region_ID = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L), .Label = c("A", "A_left", "A_right", "B", "B_left", "B_right"
    ), class = "factor"), SNP_ID = c(56789L, 56789L, 56789L, 56789L, 
    56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 45678L, 
    56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 
    56789L, 56789L, 45678L, 56789L, 56789L, 56789L, 56789L, 56789L, 
    56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 
    56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 
    45678L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 56789L, 
    56789L, 56789L, 56789L, 45678L, 56789L, 56789L, 56789L, 56789L, 
    56789L, 56789L, 56789L, 56789L, 56789L, 56789L), chr_key = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L), probe_type = structure(c(4L, 4L, 4L, 2L, 2L, 2L, 1L, 1L, 
    1L, 3L, 3L, 4L, 4L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 4L, 4L, 
    4L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 4L, 4L, 4L, 2L, 2L, 2L, 1L, 
    1L, 1L, 3L, 3L, 4L, 4L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 4L, 
    4L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L), .Label = c("non_polymorphic", 
    "preselected", "unvalidated", "validated"), class = "factor"), 
        start = c(10000L, 1L, 1L, 1L, 1252L, 1252L, 1L, 2502L, 2502L, 
        2502L, 3752L, 10000L, 10000L, 10000L, 10000L, 11408L, 11408L, 
        10000L, 12814L, 12814L, 12814L, 14221L, 10000L, 100001L, 
        100001L, 100001L, 101252L, 101252L, 100001L, 102502L, 102502L, 
        102502L, 103753L, 10000L, 1L, 1L, 1L, 1252L, 1252L, 1L, 2502L, 
        2502L, 2502L, 3752L, 10000L, 10000L, 10000L, 10000L, 11408L, 
        11408L, 10000L, 12814L, 12814L, 12814L, 14221L, 10000L, 100001L, 
        100001L, 100001L, 101252L, 101252L, 100001L, 102502L, 102502L, 
        102502L, 103753L), stop = c(100000L, 626L, 1251L, 2501L, 
        1877L, 2501L, 5000L, 3127L, 3751L, 5000L, 4376L, 100000L, 
        10704L, 11407L, 12813L, 12111L, 12813L, 15625L, 13517L, 14220L, 
        15625L, 15625L, 100000L, 100626L, 101251L, 102501L, 101877L, 
        102501L, 105001L, 103127L, 103752L, 105001L, 105001L, 100000L, 
        626L, 1251L, 2501L, 1877L, 2501L, 5000L, 3127L, 3751L, 5000L, 
        4376L, 100000L, 10704L, 11407L, 12813L, 12111L, 12813L, 15625L, 
        13517L, 14220L, 15625L, 15625L, 100000L, 100626L, 101251L, 
        102501L, 101877L, 102501L, 105001L, 103127L, 103752L, 105001L, 
        105001L), cols = c(4, 4, 4, 2, 2, 2, 1, 1, 1, 3, 3, 4, 4, 
        4, 2, 2, 2, 1, 1, 1, 3, 3, 4, 4, 4, 2, 2, 2, 1, 1, 1, 3, 
        3, 4, 4, 4, 2, 2, 2, 1, 1, 1, 3, 3, 4, 4, 4, 2, 2, 2, 1, 
        1, 1, 3, 3, 4, 4, 4, 2, 2, 2, 1, 1, 1, 3, 3)), row.names = c(NA, 
    -66L), class = "data.frame")
    

    我使用以下代码来绘制:

    add_legend <- function(...) {
      opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), 
                  mar=c(0, 0, 0, 0), new=TRUE)
      on.exit(par(opar))
      plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
      legend(...)
    }
    #CNV_df$probe_type = "non_polymorphic"
    region = unique(CNV_df$Region_ID)
    
    
    for(i in seq_along(region))
    {
    probes = CNV_df$PS_position
    probe_type = factor(CNV_df$probe_type)
    CNV_df$cols = as.numeric(as.factor(CNV_df$probe_type))
    legend.cols = as.numeric(as.factor(levels(CNV_df$probe_type)))
    #CNV select``
    
    #should also send the start and stop into PS_position 
    cols = c("black", "blue", "green", "yellow")
    #Use logarithmic scale
    par(xpd = T)
    #pdf("1MB_benchmark_plot.pdf", width = 50, height=100)
    plot(1, 1, ylim = c(0.5, length(probes)), xlim = c(min(probes) - 20, max(probes)+10),#, main = paste("Probes ", region, sep = ""), 
         xlab = "position", bty="n", type = "n", yaxt = "n", ylab = "") 
    
    begin = min(probes)
    end = max(probes)
    n = length(probes)
    
    for(i in 1:length(probes))
    {
      lines(x = c(begin, end), y = c(n+1-i, n+1-i), col = "blue", lwd = .8)
      xs = probes[1:i]
      #cols_i = cols[probe_type[1:i]]
      points(x = xs, y = rep(n+1-i, length(xs)), pch = 18, cex = 1.0, col = CNV_df$cols)
      text(i, x = -50, y = n+1-i)
    }
    }
    
    add_legend("topright", "Probe_Type", levels(probe_type), fill = legend.cols, horiz=T)
    
    dev.off()
    

    然而,这给了我6块地,而我想 A_left , A , A_right 在一个pdf绘图中, B_left , B B_right 在一个pdf绘图中,每个绘图中都有一个图例。但目前我正在为每一个独特的 Region_ID . 我怎样才能在当前的情节中解决这个问题?

    二。我能把每一种颜色 probe_type 为每个区域获取相同的颜色?

    我想知道是否有一个ggplot的方式来做整个情节?

    谢谢!

    1 回复  |  直到 7 年前
        1
  •  2
  •   Xi wa    7 年前

    如果你想用 ggplot2 然后,开始绘制 ggpubr::annotate_figure ggpubr::ggarrange 非常有用:

    您可以将三个图排列在一起,并在右侧指定相同的图例,然后通过以下方式调整整个图(三个图的组合) top =

    annotate_figure(
      ggarrange(ggplot(...),
                ggplot(...),
                ggplot(...),
                common.legend = TRUE, legend = "right"), 
      top = text_grob(title, size = 18)) 
    
    推荐文章