VegaLite.jl: size/resolution of pdf output

Is there a way to specify with VegaLite (or maybe other packages) the resolution (or file size) of the pdf output? My goal is to use the pdf figures in a LaTeX document and because I have many figures and each is quite large, the resulting pdf becomes too large. The easiest solution for me would be to reduce the resolution already when saving the VegaLite images. Is that possible?

Depends on the package. In VegaLite there are the keywords width and height to change the file size of the figure.

p = dataset("stocks") |>
       @vlplot(
           :line,
           transform=[
               {filter="datum.symbol=='GOOG'"}
           ],
           x="date:t",
           y=:price, width=800,height=800,
       )

save(p, "/tmp/salida.png")

Simply changing width and/or height you can change the size of the figure, and later with save it will use that size.

In other plots packages there is other options. For Plots.jl you can see https://github.com/JuliaPlots/Plots.jl/issues/897.

1 Like

No, I think, this is not possible in VegaLite.
width and height only change the size of the result graph in pixel, not so much the file size. If you try to save different size of the same graph you get nearly the same file sizes for a PDF file. Surely, if you save as PNG, which is a raster/pixel format, you will get larger file size.

This is because, the saved PDF is a vector format where not the pixels are saved but how to draw the graph.

So, if you save your VegaLite graphs as PDFs and than import those PDFs into your LaTeX document, you may have an option at this stage to define the resolution of your result document.

I think the VegaLite part of your problem is to save the graph as an eps, which is encapsulated post script. This format is again a vector format but it is size (paper or canvas size) independent, so it scales to what ever size/resolution you like. But for the details with LaTex I can’t tell.

1 Like

You have reason, the width and height keyworks have help me in generating PNG files, I did not see that he was talking about pdf version. @BenjaminBorn, with eps or other vectorial format you can later set the width parameter in includegraph in Latex and the figure should maintain good quality. With other backends like PGFPlots.jl or https://github.com/KristofferC/PGFPlotsX.jl you can integrate it easily in Latex (I think, I actually have not used them).

Are you sure about this? PDF is a vector format. The resolution is just a hint for mapping/rendering to physical media (screen, paper, etc) and should not affect the rest of the information in the file, or its size in any way (the only exception would be pixel images embedded in a PDF file, for which resolution could matter, but I don’ think this applies in your case).

2 Likes

So if file size (i.e. bytes on disc) is the concern, then exporting it as a PNG might be the solution, as others have pointed out.

I’ve tried to add a DPI option for the PNG export recently (see here), but am stuck on an upstream problem with that right now.

1 Like

InspectDR & LaTeX

If you are only looking for 2D plots, you could try InspectDR:

It supports EPS, PDF, and SVG as vector formats. In my experience, EPS is ideal for use with LaTeX.

Typically, vector formats tend to have file sizes that are smaller than a reasonable quality pixel-based output.

…unless the data making up the plot is large.

“F1”-acceleration (dataset is large)

In the case where the dataset is large: InspectDR supports “F1”-acceleration, which drops points when datasets are plotted without glyphs (lines only). See info on :drop_points & pointdropmatrix in the README.md file (https://github.com/ma-laforge/InspectDR.jl).

.PNG Output & DPI option

If you cannot apply “F1”-accerleration (because it would look wrong when glyphs are drawn), you can always save as a .PNG or other pixel-based format (as you could with most other plotting tool).

InspectDR DOES currently support changing the plot resolution. You can look at demo12.jl as a model:

It makes use of stylesheets defined in stylesheets.jl:

You could make your own stylesheet using the sample getstyle()/setstyle!() functions in stylesheets.jl.

Or you could just manually change the plot.layout[]/mplot.layout[] values in your plotting code (example):

lyt[:halloc_data] = 800
lyt[:valloc_data] = 500

and/or

mplot.layout[:halloc_plot] = 1920
mplot.layout[:valloc_plot] = 1080

You will need to pay attention to the points per inch and points to pixel conversion values in the sample code as well, ex:

ppi = 300
pt2px = ppi/DTPPOINTS_PER_INCH

A quick test for EPS

If you would like to see how the EPS file integrates into LaTeX, you could run one of the demo files in order to quickly generate an output file.

I suggest running sample/runsamples.jl, and using the output from demo2: export_multiplot.eps.

1 Like

Thanks everyone for the helpful replies. It seems that there is no easy solution to my problem.