The new rsvg package renders (vector based) SVG images into high-quality bitmap arrays. The resulting image is an array of 3 dimensions: height * width * 4 (RGBA) and can be written to png, jpeg or webp format:
<span># create an svg image</span>
<span>library</span><span>(</span>svglite<span>)</span>
<span>library</span><span>(</span>ggplot2<span>)</span>
svglite<span>(</span><span>"plot.svg"</span><span>,</span> width <span>=</span> <span>10</span><span>,</span> height <span>=</span> <span>7</span><span>)</span>
qplot<span>(</span>mpg<span>,</span> wt<span>,</span> data <span>=</span> mtcars<span>,</span> colour <span>=</span> <span>factor</span><span>(</span>cyl<span>))</span>
dev.off<span>()</span>
<span># render it into a bitmap array</span>
<span>library</span><span>(</span>rsvg<span>)</span>
bitmap <span><-</span> rsvg<span>(</span><span>"plot.svg"</span><span>)</span>
<span>dim</span><span>(</span>bitmap<span>)</span>
<span>## [1] 504 720 4</span>
<span># write to format</span>
png<span>::</span>writePNG<span>(</span>bitmap<span>,</span> <span>"bitmap.png"</span><span>)</span>
jpeg<span>::</span>writeJPEG<span>(</span>bitmap<span>,</span> <span>"bitmap.jpg"</span><span>,</span> quality <span>=</span> <span>1</span><span>)</span>
webp<span>::</span>write_webp<span>(</span>bitmap<span>,</span> <span>"bitmap.webp"</span><span>,</span> quality <span>=</span> <span>100</span><span>)</span>
The advantage of storing your plots in svg format is they can be rendered later ...