水平粘贴 ggplot 对象

Pasting ggplot objects horizontally

我正在尝试将 16 个不同的绘图粘贴到一个具有公共 x 轴的绘图中

我使用 grid.draw()

成功制作、绘制和粘贴了图表

但我无法以连续的方式变换 x 轴或至少删除每个图形之间的 space

我附上一些代码来说明图表是如何制作的。

plot1 = ggplot(map_snp %>% filter( chr == chr[1] ), aes(x=POS)) + 
  geom_histogram( binwidth = 2,
                  col=palette[2],
                  fill=palette[2],
                  alpha = .2) +
  xlab("Chromosome 1") +
  ylab("SNP count") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 0, colour = "black"),
        text=element_text(family="Times New Roman", size = 12),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line.y = element_line(colour = "black"),
        axis.line.x = element_line(colour = "black"))

par_plots <- list() 
for (i in list_par) {
  par_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) + 
    geom_histogram( binwidth = 2,
                    col= palette[3],
                    fill= palette[3],
                    alpha = .2) +
    xlab(paste0(i)) +
    theme_bw() +
    theme(axis.text.x = element_text(angle = 0, colour = "black"),
          text=element_text(family="Times New Roman", size = 12),
          panel.border = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line.y = element_blank(),
          axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.line.x = element_line(colour = "black"))
}
impar_plots <- list()
for (i in list_impar) {
impar_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) + 
    geom_histogram( binwidth = 2,
                    col= palette[2],
                    fill=palette[2],
                    alpha = .2) +
  xlab(paste0(i)) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 0, colour = "black"),
        text=element_text(family="Times New Roman", size = 12),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.x = element_line(colour = "black"))
}

grid.newpage()
png(paste("./snp_map.png"),  width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(plot1),
                ggplotGrob(par_plots$`Chromosome 2`),
                ggplotGrob(impar_plots$`Chromosome 3`),
                ggplotGrob(par_plots$`Chromosome 4`),
                ggplotGrob(impar_plots$`Chromosome 5`),
                ggplotGrob(par_plots$`Chromosome 6`),
                ggplotGrob(impar_plots$`Chromosome 7`),
                ggplotGrob(par_plots$`Chromosome 8`),
                ggplotGrob(impar_plots$`Chromosome 9`),
                ggplotGrob(par_plots$`Chromosome 10`),
                ggplotGrob(impar_plots$`Chromosome 11`),
                ggplotGrob(par_plots$`Chromosome 12`),
                ggplotGrob(impar_plots$`Chromosome 13`),
                ggplotGrob(par_plots$`Chromosome 14`),
                ggplotGrob(impar_plots$`Chromosome 15`),
                ggplotGrob(par_plots$`Chromosome 16`),
                size = "last"))

dev.off()

SNP 图谱

我可以去掉大部分边框,但不是全部。这里使用来自 ggplot2.

的内置 diamonds 数据集的示例
par_plots <- list() 
cuts = unique(diamonds$cut)
for (c in as.numeric(cuts)) {
  par_plots[[c]] = ggplot(diamonds %>% filter(as.numeric(cut) == c),
                          aes(x = price)) +
    geom_histogram() +
    scale_x_continuous(expand = expansion(0)) +
    xlab(paste0(i)) +
    theme_classic() +
    theme(axis.text.x = element_text(angle = 0, colour = "black"),
          text=element_text(family="Times New Roman", size = 12), 
          plot.margin = unit(c(0,0,0,0), "cm"),
          panel.border = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line.y = element_blank(),
          axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.line.x = element_line(colour = "black"))
}

library(grid)
grid.newpage()
# png(paste("./snp_map.png"),  width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(par_plots[[1]]),
                ggplotGrob(par_plots[[2]]),
                ggplotGrob(par_plots[[3]]),
                ggplotGrob(par_plots[[4]])))


dev.off()