r - 循环基础 R 以获得每个区域的 pdf 图或该图的 ggplot 替代方案

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

我。然而,这给了我 6 个图,而我想要在一个 pdf 图中 A_leftAA_rightB_leftBB_right 在一个 pdf 图中,每个图中都有一个图例。不过,目前我正在为每个唯一的 Region_ID 绘制一个。我怎样才能在当前的情节中解决这个问题?

ii.我可以修复颜色,以便每个 probe_type 在每个区域获得相同的颜色吗?

三.我很想知道是否有 ggplot 方式来完成整个情节?

谢谢!

最佳答案

如果您想使用ggplot2来绘图,那么ggpubr::annotate_figureggpubr::ggarrange会非常有帮助:

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

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

关于r - 循环基础 R 以获得每个区域的 pdf 图或该图的 ggplot 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52714327/

相关文章:

wolfram-mathematica - 绘图曲线在 X 轴下方 - 修复了吗?

r - Ubuntu下xlsx包无法设置值

r - 控制 R 中中止的函数内的工作目录

r - 在R的data.table环境中创建公式

使用多个数据帧从图中的 ggplot 图例中删除不正确的彩色轮廓?

将 ggplot 中的图例大小减小到超出默认大小

r - 将列表作为新行值插入 1 列,同时复制其他列

r - 在 R 中向自动绘图添加线条

r - 在 grid.arrange 中保留或设置文本标签与绘图大小之间的比例

python - 删除 matplotlib 中子图上的重叠刻度线