Including svg pattern fill definitions in the document allows Vega to use them in its spec.
cat( readLines("svg-defs-header.svg"), sep="\n" )
<svg height="10" width="10" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<pattern id="circles-1" patternUnits="userSpaceOnUse"
x="0" y="0" width="10" height="10" >
<rect width='10' height='10' fill="white" />
<circle cx="1" cy="1" r="1" fill="black"/>
</pattern>
<pattern id="diagonal-stripes-1" patternUnits="userSpaceOnUse" width="10" height="10">
<rect width='10' height='10' fill='white'/>
<path d='M-1,1 l2,-2
M0,10 l10,-10
M9,11 l2,-2' stroke='black' stroke-width='1'/>
</pattern>
<pattern id="horizontal-stripes-1" patternUnits="userSpaceOnUse" width="10" height="10">
<rect width='10' height='10' fill='white' />
<rect x='0' y='0' width='10' height='1' fill='black' />
</pattern>
<pattern id="vertical-stripes-1" patternUnits="userSpaceOnUse" width="10" height="10">
<rect width='10' height='10' fill='white' />
<rect x='0' y='0' width='1' height='10' fill='black' />
</pattern>
<pattern id="crosshatch" patternUnits="userSpaceOnUse" width="8" height="8">
<rect width='8' height='8' fill='#fff'/>
<path d='M0 0L8 8ZM8 0L0 8Z' stroke-width='0.5' stroke='#aaa'/>
</pattern>
</defs>
</svg>
library(ggvis)
patterns <- c("circles-1","diagonal-stripes-1", "horizontal-stripes-1", "vertical-stripes-1", "crosshatch")
patterns <- paste0("url(#", patterns, ")")
set.seed(1014)
df <- data.frame(x1 = runif(5), x2 = runif(5), y1 = runif(5), y2 = runif(5),
color=rainbow(5), pattern=patterns[c(4,1,2,5,3)])
spec <- df %>% ggvis(~x1, ~y1, x2 = ~x2, y2 = ~y2) %>%
layer_rects( fill:= ~pattern, fillOpacity := 0.6) %>%
layer_rects( fill = ~color, fillOpacity := 0.1) %>%
hide_legend("fill") %>%
set_options( width = 500, height = 500, keep_aspect=TRUE)
print( spec )
export_svg(spec, "spec.svg")
Unfortunately conversion with vg2svg fails…
We can see the problem is the svg defs fail to get pulled into the svg…
save_spec( spec, "spec.json")
vg2svg spec.json spec.svg -h
xmllint --format spec.svg > base-spec.svg
head -n 5 base-spec.svg
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="552" height="554" version="1.1">
<g transform="translate(46,6)">
<g class="type-group">
So, injecting the definitions into the svg makes them available
line=3
sed -e "${line}r svg-defs.svg" base-spec.svg > modified-spec.svg
The modified svg image…
and conversion to pdf can be done using something like
inkscape modified-spec.svg -A spec.pdf