R语言进行图片排版并添加标题

原始图片为:


R语言进行图片排版并添加标题_第1张图片
ACh4.png

共16张图片:


R语言进行图片排版并添加标题_第2张图片
files.png

截取

使用python截取16张图片中大小位置相同的区域,即去掉图片的空白区域

代码参考:https://www.jianshu.com/p/856d1d420854

from PIL import Image
import os
all_files = os.listdir()

for i in all_files:
    print(i[-4:])
    if i[-4:]!=".TIF":
        continue
    im = Image.open(i)
    '''
    裁剪:传入一个元组作为参数
    元组里的元素分别是:(距离图片左边界距离x, 
    距离图片上边界距离y,距离图片左边界距离+裁剪框宽度x+w,
    距离图片上边界距离+裁剪框高度y+h)
    '''
    x = 167
    y = 500
    w = 1300
    h = 1030
    region = im.crop((x, y, x+w, y+h))
    region.save(i.split('.')[0]+".png")

排版合并,添加标题

排版16张图片,并为16张图片添加包含latex符号的标题

实现代码:

library(cowplot)
library(latex2exp)
library(magick)
library(ggplot2)

p <- ggplot()+theme(plot.title = element_text(hjust = 0.5, vjust=4, size=6))
p1 <- p+ggtitle(label=TeX("Fig1-1 ACh: $$10^{-6.5}mol/L$$"))
p2 <- p+ggtitle(label=TeX("Fig1-2 ACh: $$10^{-6}mol/L$$"))
p3 <- p+ggtitle(label=TeX("Fig1-3 ACh: $$10^{-5.5}mol/L$$"))
p4 <- p+ggtitle(label=TeX("Fig1-4 ACh: $$10^{-5}mol/L$$"))
p5 <- p+ggtitle(label=TeX("Fig1-5 ACh: $$10^{-4.5}mol/L$$"))
p6 <- p+ggtitle(label=TeX("Fig1-6 ACh: $$10^{-4}mol/L$$"))
p7 <- p+ggtitle(label=TeX("Fig1-7 ACh: $$10^{-3.5}mol/L$$"))
p8 <- p+ggtitle(label=TeX("Fig1-8 ACh: $$10^{-3}mol/L$$"))

p9 <- p+ggtitle(label=TeX("Fig2-1 阿托品+ACh: $$10^{-6.5}mol/L$$"))
p10 <- p+ggtitle(label=TeX("Fig2-2 阿托品+ACh: $$10^{-6}mol/L$$"))
p11 <- p+ggtitle(label=TeX("Fig2-3 阿托品+ACh: $$10^{-5.5}mol/L$$"))
p12 <- p+ggtitle(label=TeX("Fig2-4 阿托品+ACh: $$10^{-5}mol/L$$"))
p13 <- p+ggtitle(label=TeX("Fig2-5 阿托品+ACh: $$10^{-4.5}mol/L$$"))
p14 <- p+ggtitle(label=TeX("Fig2-6 阿托品+ACh: $$10^{-4}mol/L$$"))
p15 <- p+ggtitle(label=TeX("Fig2-7 阿托品+ACh: $$10^{-3.5}mol/L$$"))
p16 <- p+ggtitle(label=TeX("Fig2-8 阿托品+ACh: $$10^{-3}mol/L$$"))

merged_p <- ggdraw() +
  draw_image("ACH1.png", x=0, y=0.75, width=0.2, height=0.2) +
  draw_image("ACH2.png", x=0.25, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH3.png", x=0.5, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH4.png", x=0.75, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH5.png", x=0.0, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH6.png", x=0.25, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH7.png", x=0.5, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH8.png", x=0.75, y=0.5, width=0.2, height=0.2)+
  draw_image("a1.png", x=0, y=0.25, width=0.2, height=0.2) +
  draw_image("a2.png", x=0.25, y=0.25, width=0.2, height=0.2) +
  draw_image("a3.png", x=0.5, y=0.25, width=0.2, height=0.2) +
  draw_image("a4.png", x=0.75, y=0.25, width=0.2, height=0.2) +
  draw_image("a5.png", x=0, y=0, width=0.2, height=0.2) +
  draw_image("a6.png", x=0.25, y=0, width=0.2, height=0.2) +
  draw_image("a7.png", x=0.5, y=0, width=0.2, height=0.2) +
  draw_image("a8.png", x=0.75, y=0, width=0.2, height=0.2)+
  draw_image("ACH1.png", x=0, y=0.75, width=0.2, height=0.2) +
  draw_image("ACH2.png", x=0.25, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH3.png", x=0.5, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH4.png", x=0.75, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH5.png", x=0.0, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH6.png", x=0.25, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH7.png", x=0.5, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH8.png", x=0.75, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH1.png", x=0, y=0.75, width=0.2, height=0.2) +
  draw_image("ACH2.png", x=0.25, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH3.png", x=0.5, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH4.png", x=0.75, y=0.75, width=0.2, height=0.2)+
  draw_image("ACH5.png", x=0.0, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH6.png", x=0.25, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH7.png", x=0.5, y=0.5, width=0.2, height=0.2)+
  draw_image("ACH8.png", x=0.75, y=0.5, width=0.2, height=0.2)+
  draw_plot(p9, x=0, y=0.25, width=0.2, height=0.2) +
  draw_plot(p10, x=0.25, y=0.25, width=0.2, height=0.2) +
  draw_plot(p11, x=0.5, y=0.25, width=0.2, height=0.2) +
  draw_plot(p12, x=0.75, y=0.25, width=0.2, height=0.2) +
  draw_plot(p13, x=0, y=0, width=0.2, height=0.2) +
  draw_plot(p14, x=0.25, y=0, width=0.2, height=0.2) +
  draw_plot(p15, x=0.5, y=0, width=0.2, height=0.2) +
  draw_plot(p16, x=0.75, y=0, width=0.2, height=0.2)+
  draw_plot(p1, x=0, y=0.75, width=0.2, height=0.2) +
  draw_plot(p2, x=0.25, y=0.75, width=0.2, height=0.2)+
  draw_plot(p3, x=0.5, y=0.75, width=0.2, height=0.2)+
  draw_plot(p4, x=0.75, y=0.75, width=0.2, height=0.2)+
  draw_plot(p5, x=0.0, y=0.5, width=0.2, height=0.2)+
  draw_plot(p6, x=0.25, y=0.5, width=0.2, height=0.2)+
  draw_plot(p7, x=0.5, y=0.5, width=0.2, height=0.2)+
  draw_plot(p8, x=0.75, y=0.5, width=0.2, height=0.2)

pdf("merged.pdf", height=11, width=12)
merged_p
dev.off()

效果如下:


R语言进行图片排版并添加标题_第3张图片
merged.png

你可能感兴趣的:(R语言进行图片排版并添加标题)