Reading in multiple png files in order to create a new plot with grid.arrange

前端 未结 2 1374
北荒
北荒 2021-01-06 02:51

Let\'s say that I just created 4 histograms in ggplot2. After finishing, I realized that I should have used grid.arrange to combine some of the plots into a single graphics

相关标签:
2条回答
  • 2021-01-06 03:27
    rl = lapply(sprintf("my_viz%i.png", 1:4), png::readPNG)
    gl = lapply(rl, grid::rasterGrob)
    gridExtra::grid.arrange(grobs=gl)
    
    0 讨论(0)
  • 2021-01-06 03:30

    Personally, I would have gone to some length to avoid reimporting the saved graphics, and I would have rerun the visualisations again, if at all possible, in order to have code that is guaranteed to produce the same results next time. It is always a risk to rely on that external files are not accidentally deleted or modified.

    However, sometimes you need a really quick hack, and if that is your situation right now, try this:

    library("png")
    library("raster")
    plot(0:2, 0:2, type = "n", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
    rasterImage(readPNG(source="my_viz1.png"), 0, 1, 1, 2)
    rasterImage(readPNG(source="my_viz2.png"), 1, 1, 2, 2)
    rasterImage(readPNG(source="my_viz3.png"), 0, 0, 1, 1)
    rasterImage(readPNG(source="my_viz4.png"), 1, 0, 2, 1)
    
    0 讨论(0)
提交回复
热议问题