Plotting SVG State Flags in R

Pre-requisites

Method

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

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