Plotting SVG State Flags in R
Pre-requisites
Method
- We would like to plot each of the 50 state flags in R. SVG flags of the 50 states were downloaded from Wikimedia.
- These flags were then converted to PostScript and SVG via Cairo. This is performed entirely using the
convertPicture()
function in the grConvert package. librsvg was used to read in the SVG image, which is then rendered to the appropriate output format by Cairo.
- Once the source images have been converted, they are now available for rendering via grImport and grImport2. Both sources are imported into R and saved to a
pdf()
device. In the case of grImport2, because it uses SVG as its source image it can use features that are not available in PostScript (e.g. semi-transparency).
- We can also use gridSVG to render images imported by grImport2, which allows us to use graphical features not present in R graphics. To do this, instead of rendering to the
pdf()
device, we just use the gridsvg()
device or grid.export()
instead.
grImport has been improved to handle rectangle fills and strokes, given this improvement many of the images are now drawn correctly. In addition, the line endings, line join and line mitre properties of strokes are now respected.
Notes
All images on this page are just PNG thumbnails from the source images. To view the source images, click on the thumbnail.
The thumbnails for the PDF images were created by the evince-thumbnailer program. For SVG images, inkscape was used instead.
Thumbnails created by evince-thumbnailer appear to exaggerate the strokes on images slightly, so the thumbnails may appear to be slightly incorrect in comparison to the source image (at least for the grImport and grImport2 columns).
Further Work
- DONE grImport: Support for rectangles.
- DONE grImport2: More complete support for importing images such as those featuring patterns, masks, rasters and filters. Partial support is already available for rasters and patterns but this has not yet been tested thoroughly.
- grImport2: Polishing the high-level interface for using pictures. Just need to ensure that symbols work correctly (not just individual pictures).
- DONE grImport2: Add support for converting images to Cairo SVG. There is currently R code to perform this task, but it not yet calling C code to actually do the required work. There is now a grConvert package which converts pictures into Cairo SVG for use with grImport and grImport2.
- DONE grImport2: Package documentation.
- DONE grImport2: Test with other image sources, such as PDF and PostScript.
- grImport2: Attempt to get grImport2 building only multiple platforms (probably requires Autotools at some point). This is almost done. We have a way of neatly distributing the grConvert package for use in Linux, but installing on OSX and Windows may be more problematic. grConvert is not strictly necessary for using grImport2 so grImport2 still works.
Known Limitations
Some images cannot be parsed correctly from their SVG form because of the SVG attribute "stroke-dasharray". R's equivalent, "lty" is too restrictive because instead of allowing any non-negative value, it only allows values between 0x1 and 0xF (1 and 15). Furthermore, it only allows a maximum of 8 values, whereas "stroke-dasharray" is does not feature this limitation. This limitation cannot (reasonably) be worked around in R but the image could be "flattened" using other software (and then read in again as Cairo SVG).
There are other features of SVG that are not available in R graphics such as non-rectangular clipping paths, gradients (linear and radial), masks, filters, etc. Consequently, these features cannot be used on a regular R graphics device. We can use gridSVG to enable these features.
Table Key
- SVG
- The original SVG image taken from Wikimedia.
- grImport
- The original SVG image, converted to PostScript via grConvert, which is then imported via grImport and rendered to PDF.
- Improved grImport
- grImport with support for rectangles added (via support for
/rectfill
and /rectstroke
). This corrects the rendering of many flags. Additionally, there is now support for more line properties: line endings, line joins, and line mitres.
- grImport2
- The SVG image, converted to Cairo SVG via grConvert, then imported via grImport2 and rendered to PDF. All clipping paths are based on the bounding box of the clipping path (because R can only create rectangular clipping paths).
- grImport2 + gridSVG
- The SVG image, converted to Cairo SVG via grConvert, then imported via grImport2 and rendered back to SVG via gridSVG. This means we can use the necessary features to render the flags correctly, which are gradients and (non-rectangular) clipping paths.
State |
SVG |
grImport |
Improved grImport |
grImport2 |
grImport2 + gridSVG |
Notes |
Alabama |
|
|
|
|
|
|
Alaska |
|
|
|
|
|
|
Arizona |
|
|
|
|
|
This cannot be parsed correctly in its SVG form because the red background is "striped" because of the SVG attribute "stroke-dasharray". R's equivalent, "lty" is too restrictive. See "Known Limitations". |
Arkansas |
|
|
|
|
|
|
California |
|
|
|
|
|
|
Colorado |
|
|
|
|
|
|
Connecticut |
|
|
|
|
|
|
Delaware |
|
|
|
|
|
|
Florida |
|
|
|
|
|
|
Georgia |
|
|
|
|
|
Features a non-rectangular clipping path. |
Hawaii |
|
|
|
|
|
|
Idaho |
|
|
|
|
|
|
Illinois |
|
|
|
|
|
|
Indiana |
|
|
|
|
|
|
Iowa |
|
|
|
|
|
|
Kansas |
|
|
|
|
|
Features many linear gradients that comprise the centre of the flag. |
Kentucky |
|
|
|
|
|
|
Louisiana |
|
|
|
|
|
|
Maine |
|
|
|
|
|
|
Maryland |
|
|
|
|
|
|
Massachusetts |
|
|
|
|
|
|
Michigan |
|
|
|
|
|
|
Minnesota |
|
|
|
|
|
|
Mississippi |
|
|
|
|
|
|
Missouri |
|
|
|
|
|
|
Montana |
|
|
|
|
|
|
Nebraska |
|
|
|
|
|
|
Nevada |
|
|
|
|
|
Features radial gradients, but they are so small that their effect is negligible. gridSVG does use the gradients and exports them but the flag appears fine with the radial gradients omitted. |
New Hampshire |
|
|
|
|
|
|
New Jersey |
|
|
|
|
|
|
New Mexico |
|
|
|
|
|
|
New York |
|
|
|
|
|
|
North Carolina |
|
|
|
|
|
|
North Dakota |
|
|
|
|
|
|
Ohio |
|
|
|
|
|
Features a non-rectangular clipping path. |
Oklahoma |
|
|
|
|
|
|
Oregon |
|
|
|
|
|
|
Pennsylvania |
|
|
|
|
|
|
Rhode Island |
|
|
|
|
|
|
South Carolina |
|
|
|
|
|
grImport imports and renders this flag, but this is a white foreground on a non-existent blue background. Therefore this appears as white on white. |
South Dakota |
|
|
|
|
|
|
Tennessee |
|
|
|
|
|
|
Texas |
|
|
|
|
|
|
Utah |
|
|
|
|
|
|
Vermont |
|
|
|
|
|
|
Virginia |
|
|
|
|
|
|
Washington |
|
|
|
|
|
|
West Virginia |
|
|
|
|
|
|
Wisconsin |
|
|
|
|
|
|
Wyoming |
|
|
|
|
|
|