0

Objective

Read in a svg/postscript file and plot it using ggplot2

My current workflow

I have the svg file here if you want to download it. I first convert it into a postscript (using an online tool) and then into an xml file and then plot it as follows:

Sys.setenv(R_GSCMD = normalizePath("C:/Program Files/PDFCreator/Ghostscript/Bin/gswin32c.exe")) 

library(grImport)
PostScriptTrace(file = "road_network.ps",
                outfilename = "road_network.ps.xml")
lc <- readPicture("road_network.ps.xml")

## plot:
grid.picture(lc)

This results in:

enter image description here

What I want to do

I want to be able to zoom into specific sections of the plot, etc. So, I want to convert the picture data into a dataframe to use ggplot2. The lc picture data created above looks like following:

head(lc)
An object of class "Picture"
Slot "paths":
$path
An object of class "PictureFill"
Slot "rule":
[1] "nonzero"

Slot "x":
   move    line    line    line    line    line 
666.844 675.328 686.910 686.910 675.309 666.844 
   line 
666.844 

Slot "y":
   move    line    line    line    line    line 
9894.32 9894.41 9895.16 9899.84 9900.34 9900.68 
   line 
9894.32 

Slot "rgb":
[1] "#808080"

Slot "lty":
numeric(0)

Slot "lwd":
[1] 0.220724

Slot "lineend":
[1] 1

Slot "linejoin":
[1] 1

Slot "linemitre":
[1] 10



Slot "summary":
An object of class "PictureSummary"
Slot "numPaths":
[1] 1

Slot "xscale":
[1] 666.844 686.910

Slot "yscale":
[1] 9894.32 9900.68

How do I convert it into a dataframe?

umair durrani
  • 5,597
  • 8
  • 45
  • 85
  • Can you save it as an SVG? It looks like there are existing ways to bring an SVG into a ggplot that might be simpler, e.g. https://stackoverflow.com/q/61387217/6851825 – Jon Spring Nov 19 '21 at 20:32
  • @JonSpring, the link you shared is about annotating svg data as image points. However, I want to convert picture data to dataframe – umair durrani Nov 19 '21 at 20:43

0 Answers0