在表格中排列微观图像以供发布

问题描述 投票:1回答:1

[我想知道是否存在一种自动的方法来将R中的多个显微图像对齐,以这种图像here的形式出现(来源:doi:https://doi.org/10.1101/2019.12.11.873471)。我不是在寻找图像分析,只是将图像网格化并添加标签。我尝试使用magick在其中可以在图像上写标签,并尝试使用gridExtra对齐它们并在外部添加标签。但仍不满意,因为我无法控制块之间的距离,无法在上面的图像中添加标签。有人可以推荐一个软件包来做这样的事情吗(也许在R或Python中,不确定Latex是否也可以做类似的事情?)。以下是我的可复制代码以及得到的内容。非常感谢您的帮助。


library(magick)
library(grid)
library(gridExtra)


names_of_images <- LETTERS[1:16]


pic_url <- "https://imagehost.imageupload.net/2020/04/30/EXAMPLE.jpg"
pic_cat <- tempfile()
download.file(pic_url,pic_cat,mode="wb")
pic <- image_read(pic_cat)

## write labels on images

image_listall <-  list()

for ( i in names_of_images ) {

 im_read_bor <- image_border(pic , 'white'  ,geometry = "10x10")  

 im_read_bor_anno <-  image_annotate(im_read_bor, paste(i),

   size = 100, color = "white" , location = "+50+40" )

 image_listall[[i]] = im_read_bor_anno

}


## arrange images in rows

row1 <- image_append(c(image_listall$A, image_listall$B ,image_listall$C,image_listall$D  ), stack = F)

row2 <- image_append(c(image_listall$E, image_listall$F ,image_listall$G,image_listall$H  ), stack = F)

row3 <- image_append(c(image_listall$I, image_listall$J,image_listall$K,image_listall$L) ,stack = F)

row4 <- image_append(c(image_listall$M, image_listall$N,image_listall$O, image_listall$P) ,stack = F)


## now add row labels and title


r1 <- grid.arrange(rasterGrob(row1) , top = textGrob(
 "First Block",just = "center",
 gp = gpar(fontface = 'bold', fontsize = 18)) ,  

 left = textGrob(
   "(A)",
   gp = gpar(fontface = 'bold', fontsize = 15)))


r2 <- grid.arrange(rasterGrob(row2) , 

                  left = textGrob(
                    "(B)",
                    gp = gpar(fontface = 'bold', fontsize = 15)))


r3 <- grid.arrange(rasterGrob(row3) , top = textGrob(
 "Second Block", just = "center",
 gp = gpar(fontface = 'bold', fontsize = 18)) ,  

 left = textGrob(
   "(C)",
   gp = gpar(fontface = 'bold', fontsize = 15)))





r4 <- grid.arrange(rasterGrob(row4) , 

                  left = textGrob(
                    "(D)",
                    gp = gpar(fontface = 'bold', fontsize = 15)))


## draw all together 

grid.draw(rbind(r1, r2,r3,r4, size = "last"))

它看起来像这样:

enter image description here

r image latex
1个回答
0
投票

也许不是最好且更优雅的解决方案,但一种可能的方法是使用facet_grid中的函数ggplot2绘制多面板图。使用ggplot2的优点是,您可以受益于大量工具,这些工具非常易于使用,可以自定义所有标签的位置,字体和颜色。

因此,您可以为每个垂直和水平标签准备一个带有文本的假数据框:

verticallabel <- c("Control", "cKO")
horizontallabel <- c("Text1","Text2","Text3")
text <- expand.grid(verticallabel,horizontallabel)
text <- as.data.frame(text)

     Var1  Var2
1 Control Text1
2     cKO Text1
3 Control Text2
4     cKO Text2
5 Control Text3
6     cKO Text3

然后,您可以创建一个带有6个带有水平和垂直标签的面板的空图,如下所示:

library(ggplot2)

g <- ggplot(text, aes(x = Var1, y = Var2))+geom_point(color = NA)+
  facet_grid(Var1~Var2, switch = "y")+  labs(tag = "A")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        panel.background = element_blank(),
        axis.ticks = element_blank(),
        plot.margin = margin(1,1,1,1, unit = "cm"),
        plot.tag.position = c(-0.015, 1.05))+
  coord_cartesian(clip = "off", ylim = c(0,5), xlim = c(0,2))+
  geom_text(data = subset(text, Var1 == "Control"), aes(label = Var2, x = 1, y = 6.3, color = Var2), show.legend = FALSE,vjust = 0.5)+
  geom_text(data = subset(text, Var2 == "Text1"), aes(label = Var1, x = -0.8, y = 2.5, angle = 90, vjust = 0))+
  geom_rect(data = subset(text, Var1 == "Control"), aes(xmin = -Inf, xmax = Inf, ymax = 6.8, ymin = 5.8), fill = NA, color = "black")+
  geom_rect(data = subset(text, Var2 == "Text1"), aes(ymin = -Inf, ymax = Inf, xmin = -1, xmax = -0.7), fill = NA, color = "black")

enter image description here

然后,如果要在每个面板上添加图像,则可以使用@EdgarSantos在本文中提出的出色解决方案:Adding custom images to ggplot facets

annotation_custom2 <- 
  function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data){ layer(data = data, stat = StatIdentity, position = PositionIdentity, 
                                                                                 geom = ggplot2:::GeomCustomAnn,
                                                                                 inherit.aes = TRUE, params = list(grob = grob, 
                                                                                                                   xmin = xmin, xmax = xmax, 
                                                                                                                   ymin = ymin, ymax = ymax))}

这里有一些示例图像(其中一些是从我的计算机上下载的,因此您需要调整图像的路径):

library(png)

img1 = readPNG(getURLContent('https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png'))
img2 = readPNG(getURLContent('https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png'))
img3 = readPNG(getURLContent('https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png'))

rlogo <- readPNG("../external-content.duckduckgo.com.png")
rstudio <- readPNG("../rstudio.png")

g +
  annotation_custom2(rasterGrob(img1, width =unit(1,"npc"), height = unit(1,"npc")),
                     xmin = -Inf, xmax =Inf, ymin = -Inf, ymax = Inf, data = text[1,])+
  annotation_custom2(rasterGrob(img2, width =unit(1,"npc"), height = unit(1,"npc")),
                     xmin = -Inf, xmax =Inf, ymin = -Inf, ymax = Inf, data = text[2,])+
  annotation_custom2(rasterGrob(img3, width =unit(1,"npc"), height = unit(1,"npc")),
                     xmin = -Inf, xmax =Inf, ymin = -Inf, ymax = Inf, data = text[3,])+
  annotation_custom2(rasterGrob(rlogo, width =unit(1,"npc"), height = unit(1,"npc")),
                     xmin = -Inf, xmax =Inf, ymin = -Inf, ymax = Inf, data = text[4,])+
  annotation_custom2(rasterGrob(rstudio, width =unit(1,"npc"), height = unit(1,"npc")),
                     xmin = -Inf, xmax =Inf, ymin = -Inf, ymax = Inf, data = text[5,])

enter image description here

它回答了您的问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.