Last active
March 3, 2020 03:02
-
-
Save baptiste/311177e0175f3e9b87caaac9bc41b7ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<svg width="200mm" height="200mm" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> | |
<rect x="22.125" width="160.967" height="77.396" style="fill: rgb(216, 216, 216);" id="plot1" y="5.586"/> | |
<rect x="73.764" y="99.91" width="109.328" height="87.61" style="fill: rgb(216, 216, 216);" id="plot2"/> | |
<rect x="22.125" y="99.91" width="29.899" height="87.61" style="fill: rgb(216, 216, 216);" id="plot3"/> | |
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(minisvg) | |
library(purrr) | |
library(dplyr) | |
library(egg) | |
library(grid) | |
make_viewport <- function(x, y, width, height, id, ...){ | |
viewport(x = unit(x,'mm'), | |
y = unit(1,'npc') - unit(height,'mm') - unit(y,'mm'), | |
width=unit(width,'mm'), | |
height=unit(height,'mm'), just = c(0,0), | |
clip = 'off', name = id) | |
} | |
draw_frame <- function(g, vp){ | |
pushViewport(vp) | |
innervp <- viewport(width=vp$width + sum(g$widths[c(1,3)]), | |
height=vp$height + sum(g$heights[c(1,3)]), | |
x=-1*g$widths[1], | |
y=-1*g$heights[3], | |
just = c(0,0)) | |
pushViewport(innervp) | |
grid.draw(g) | |
upViewport() | |
grid.rect(gp=gpar(fill=NA, lty=2,col='red')) | |
upViewport() | |
} | |
plot_with_layout <- function(plots=list(p,p,p), | |
layout='layout.svg'){ | |
doc <- minisvg::parse_svg_doc(layout) | |
v <- map_df(doc$children, `$`, 'attribs') %>% | |
select(c('x','y','width','height','id')) %>% | |
mutate_at(.vars = vars(!matches("^id$")), as.numeric) | |
v$vp <- pmap(v, make_viewport) | |
gl <- lapply(plots, ggplotGrob) | |
gl <- lapply(gl, egg::gtable_frame, debug = TRUE) | |
walk2(gl, v$vp, draw_frame) | |
} | |
svg('test.svg',width = 200/25.4, height=200/25.4) | |
grid.rect() | |
plot_with_layout() | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment