1

I am using svglite to save SVG files of R plots.

I get SVG files that have height and width in them. Unless I really misunderstand the docs, this is not what I expect.

https://svglite.r-lib.org/articles/scaling.html

The SVG files produced by svglite do not include width and height properties.

Possibly I am doing something stupid as I don't understand R graphics devices. I am running svglite 2.1, r 4.1.2, Rstudio 2022.07.1, on windows 10.

When I run this, basically the same as the svglite sample code:

svglite::svglite(filename = "C:/temp/ggplot.svg")
plot(1:10)
dev.off()

I get a file that does have height and width as well as viewport.

e.g.,

<?xml version='1.0' encoding='UTF-8' ?>
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'>

I get similar results with ggplot, which is what I really want to use. I have also tried using ggsave (which is the top hit on stack overflow for saving ggplot to SVG) but I still get the height and width.

any help appreciated. Thanks!

No More Hacks
  • 299
  • 4
  • 14
  • why is having height and width a problem? You can always remove the attributes after generation if you need/want to. – Robert Longson Oct 24 '22 at 12:37
  • Hi, thanks for suggestion. How would you do that in R? I'm not writing any JS and I don't think I can get over it with CSS or by putting attributes on a div or svg that wraps around it? – No More Hacks Oct 24 '22 at 15:31
  • presumably R has an XML parser so parse the document and remove the appropriate attributes. – Robert Longson Oct 24 '22 at 15:40
  • 1
    I agree it could be clearer, but the webpage you cite includes the statement: "The viewBox is determined by the `width` and` height` arguments of svglite’s device functions (with 10’’ x 8’’ the default). Although those dimensions are supplied in inches, the viewBox’s user coordinate system is completely unit agnostic. The main effect is thus to determine an aspect ratio. Since dimensions are not provided, the dimensions of the enclosing container are used instead and the SVG is rescaled to fit the container ". – dcarlson Oct 24 '22 at 18:57
  • @RobertLongson That is a fair comment! I could do that. I should just solve the problem rather than trying to scratch my itch on why it doesn't work like i think it should. Thanks. – No More Hacks Oct 25 '22 at 08:01
  • @dcarlson:thanks.. to be completely clear I'm know little CSS/HTML.I interpreted that sentence as: you give a height and width to the print device and those will set the viewBox size but NOT the height and width, therefore the effect is the viewBox sets the aspect ratio but the actual height and width on the svg is not set, therefore the svg inherits the size of the parent container. I have found that the svg generated does not respect the size of the parent container, which I *believe* is due to the absolute height/width attribs being set on the svg. Is possible I know nothing. :) – No More Hacks Oct 25 '22 at 14:33

0 Answers0