在基数R上循环以获取每个区域的pdf图或该图的ggplot替代

时间:2018-10-09 06:05:18

标签: r ggplot2 plot

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

    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()

i。但是,这给了我6个图,而我想在一个pdf图中将A_leftAA_rightB_leftBB_right在一个pdf图中,每个图例都有一个图例。但是,目前我正在为每个唯一的Region_ID绘制一个。如何在当前绘图中解决此问题?

ii。我可以固定颜色以使每个probe_type的每个区域都具有相同的颜色吗?

iii。我想知道是否有一种ggplot方式可以完成整个绘图?

谢谢!

1 个答案:

答案 0 :(得分:2)

如果您想使用$(".click-on").click(function(){ $(".box").slideToggle(1000) } 进行绘制,那么ggplot2ggpubr::annotate_figure可能会非常有用:

您可以将3个地块排列在一起,并在右侧分配相同的图例,并通过ggpubr::ggarrange对整个地块(3个地块的组合)进行调整

top =