I would like to add SVG images to plots created in R.
The SVG images are chemical structures, exported from ChemDraw.
We are talking about 200 structures, meaning, it would be nice if I could automate this indeed.
The resulting plots will be saved to SVGs as well, as is straight-forward to do in R using e.g. svglite
.
The general consensus when Googling for this, is, that one should use the library grImport2
. There are few tutorials out there, doing exactly what I want.
However, I could not get it to work with any of the SVGs I have. In fact, mostly, grImport2
seems to be hanging / stuck in some infinite loop.
Here an exemplaric SVG:
<!-- Acemethacin.svg -->
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="327px" height="294px" viewBox="0 0 327 294" style="background-color: #ffffff00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" >
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 79.4583 162.934)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >N</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 31.2589 128.319)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >O</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 145.568 21.8469)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >Cl</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 30.3819 289.386)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >O</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 174.602 169.817)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >O</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 204.047 220.817)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >O</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 262.937 254.817)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >O</text>
<text x="0" y="0" stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 292.382 203.817)" font-style="normal" font-weight="normal" font-size="200px" font-family="Arial" >OH</text>
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5420.08,5216.57 L 5430.8,5228.47 L 5180.96,5453.17 L 5172.77,5450.03 L 5170.4,5441.63 L 5420.08,5216.57 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5165.64,5456.29 L 5172.77,5450.03 L 5180.96,5453.17 L 5215.67,5783.39 L 5208.31,5788.17 L 5200.02,5783.38 L 5165.64,5456.29 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5108.59,5501.48 L 5124.52,5499.81 L 5150.76,5749.5 L 5134.83,5751.18 L 5108.59,5501.48 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5200.02,5783.38 L 5208.31,5788.17 L 5207.85,5796.93 L 4904.08,5932.2 L 4897.7,5926.46 L 4899.69,5917.09 L 5200.02,5783.38 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4899.69,5917.09 L 4897.7,5926.46 L 4888.9,5928.33 L 4667.42,5682.35 L 4670.2,5673.79 L 4679.59,5672.63 L 4899.69,5917.09 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4834.53,5935.79 L 4822.63,5946.51 L 4642.41,5746.35 L 4654.31,5735.64 L 4834.53,5935.79 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4679.59,5672.63 L 4670.2,5673.79 L 4665.32,5666.8 L 4763.3,5496.52 L 4777.17,5504.53 L 4679.59,5672.63 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5170.4,5441.63 L 5172.77,5450.03 L 5165.64,5456.29 L 4949.18,5410.69 L 4952.51,5395.02 L 5170.4,5441.63 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4796.56,5261.64 L 4781.93,5268.16 L 4692.37,5067.85 L 4710.78,5069.8 L 4796.56,5261.64 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4717.12,5093.05 L 4715.44,5108.98 L 4471.96,5083.38 L 4473.63,5067.46 L 4717.12,5093.05 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4723.51,5032.18 L 4721.84,5048.11 L 4478.36,5022.52 L 4480.03,5006.59 L 4723.51,5032.18 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4710.78,5069.8 L 4692.37,5067.85 L 4892.8,4792.73 L 4901.76,4793.67 L 4905.42,4801.89 L 4710.78,5069.8 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4905.42,4801.89 L 4901.76,4793.67 L 4907.05,4786.38 L 5236.23,4820.98 L 5243.76,4837.89 L 4905.42,4801.89 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4942.46,4744.46 L 4944.13,4728.53 L 5211.99,4756.68 L 5210.31,4772.61 L 4942.46,4744.46 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5243.76,4837.89 L 5236.23,4820.98 L 5430.79,4553.2 L 5449.19,4555.13 L 5243.76,4837.89 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5449.19,4555.13 L 5430.79,4553.2 L 5296.16,4250.82 L 5301.45,4243.54 L 5310.35,4244.34 L 5449.19,4555.13 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5376.78,4543.5 L 5362.14,4550.01 L 5252.6,4303.96 L 5267.23,4297.45 L 5376.78,4543.5 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5297.94,4235.32 L 5301.45,4243.54 L 5296.16,4250.82 L 4966.97,4216.22 L 4959.45,4199.31 L 5297.94,4235.32 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4959.45,4199.31 L 4966.97,4216.22 L 4772.42,4484 L 4754.01,4482.07 L 4959.45,4199.31 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4985.58,4267.85 L 4998.53,4277.26 L 4840.22,4495.15 L 4827.27,4485.74 L 4985.58,4267.85 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4907.05,4786.38 L 4901.76,4793.67 L 4892.8,4792.73 L 4754.01,4482.07 L 4772.42,4484 L 4907.05,4786.38 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5310.35,4244.34 L 5301.45,4243.54 L 5297.94,4235.32 L 5422.47,4063.35 L 5435.42,4072.76 L 5310.35,4244.34 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4665.32,5666.8 L 4670.2,5673.79 L 4667.42,5682.35 L 4343.65,5751.17 L 4331.27,5737.41 L 4665.32,5666.8 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4331.27,5737.41 L 4343.65,5751.17 L 4241.37,6065.96 L 4223.27,6069.81 L 4331.27,5737.41 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4377.3,5794.52 L 4392.53,5799.47 L 4309.3,6055.62 L 4294.07,6050.67 L 4377.3,5794.52 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4223.27,6069.81 L 4241.37,6065.96 L 4462.85,6311.94 L 4460.07,6320.5 L 4451.34,6322.48 L 4223.27,6069.81 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4465.97,6327.23 L 4460.07,6320.5 L 4462.85,6311.94 L 4786.61,6243.13 L 4799,6256.88 L 4465.97,6327.23 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4483.57,6261.13 L 4480.24,6245.46 L 4743.69,6189.47 L 4747.02,6205.13 L 4483.57,6261.13 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4888.9,5928.33 L 4897.7,5926.46 L 4904.08,5932.2 L 4799,6256.88 L 4786.61,6243.13 L 4888.9,5928.33 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4451.34,6322.48 L 4460.07,6320.5 L 4465.97,6327.23 L 4396.88,6540.9 L 4381.64,6535.95 L 4451.34,6322.48 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 4240.55,6660 L 4243.88,6675.67 L 4024.1,6722.39 L 4020.77,6706.72 L 4240.55,6660 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5207.85,5796.93 L 5208.31,5788.17 L 5215.67,5783.39 L 5502.76,5949.16 L 5502.76,5967.67 L 5207.85,5796.93 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5502.76,5967.67 L 5502.76,5949.16 L 5797.31,5778.6 L 5797.32,5797.11 L 5502.76,5967.67 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5774.61,5805.83 L 5758.6,5805.83 L 5758.6,5572.83 L 5774.61,5572.83 L 5774.61,5805.83 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5835.81,5805.83 L 5819.8,5805.83 L 5819.8,5572.83 L 5835.81,5572.83 L 5835.81,5805.83 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 5797.32,5797.11 L 5797.31,5778.6 L 5998.92,5895.38 L 5990.91,5909.25 L 5797.32,5797.11 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 6192.73,5909.06 L 6184.72,5895.19 L 6386,5778.6 L 6385.99,5797.11 L 6192.73,5909.06 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 6385.99,5797.11 L 6386,5778.6 L 6680.68,5949.23 L 6680.66,5967.74 L 6385.99,5797.11 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 6703.14,5940.5 L 6719.16,5940.5 L 6719.16,6187.83 L 6703.14,6187.83 L 6703.14,5940.5 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 6641.94,5940.5 L 6657.96,5940.5 L 6657.96,6187.83 L 6641.94,6187.83 L 6641.94,5940.5 Z " />
<path stroke="none" fill="#000000" transform="matrix(0.1 0 0 0.1 -397.34 -382.8)" d="M 6680.66,5967.74 L 6680.68,5949.23 L 6867.18,5841.17 L 6875.19,5855.04 L 6680.66,5967.74 Z " />
</svg>
Which could be used in R like so (minimum viable reproducible example):
library(grImport2)
pic <- readPicture("Acemethacin.svg")
grid.picture(pic)
Which, on three devices I tried, gets stuck already at readPicture
.
As far as I have seen, grImport2
is archived since 2017, indicating that it may not be compatible with current R versions.
What am I doing wrong, or, what alternative is there to include an SVG into an R plot?