R: importing and saving SVG graphics - r

I have a complex task: to merge existing SVG image with barplots in R. I want to save the output file as a vector graphics as well. So I create the layout, in one of the subplots I create the barplot and now:
How can I load an existing SVG from the hard drive and then put it into the plot? I have tried grimport and grimport2 libraries but they fail to read my SVG. How should I prepare it? It is a simple sketch made in Inkscape, should I save it in any special way?
I'd prefer to use a library that is supported by Anaconda Cloud.
EDIT:
I menaged to read the .ps file with grimport and convert it to a picture object - it was crashing previously because I had a text box with non-standard font in the SVG and the library could not ready that properly (some encoding problems).
Now I am just looking for a way to put the Picture object on the layout, just as if I would use plot(runif(10), runif(10)) to have a scatterplot

grid.picture(picture_object[-1],x=x_coord,y=y_coord)
With the variables x_coord and y_coord I can manipulate the position of the image.

Related

Modify SVG width from within R after export from plotly orca

My goal is to save out an SVG file of a plotly graph, where the width argument is '100%' and static height is removed.
As it is, plotly orca (the tool for saving plotly objects) only supports specifying static pixel widths and heights.
The widths and heights set are important throughout the chart generation for maintaining proportions (It is an intricate plot combining many traces on both cartesian and polar co-ordinates).
This means the svg file saved is fixed width.
All processing has to occur within R. Is there a way to modify a text file from R commands? So far, my search has not yielded anything useful.
I have found my solution.
I am using readLines() to load my .svg file in to R.
Modify the offending style attribute using gsub. This works for me as the change I want to make is fixed and very simple.
I use writeLines() to over-write the old with the new.

Is there a way to save and quickly reopen surface plots in IDL (Interactive Data Language)?

I was wondering if there is a way to save surface plots in IDL in a way that is convenient to reopen them. As far as I am aware there are only two ways of doing this:
The first way is to save your surface plot as an image e.g. png. The trouble with this is that the viewing angle is fixed and you can no longer drag with the mouse to rotate the surface plot.
The second way is to save the code which generates the plot. Trouble with this is the code often generates loads of plots which are usually just saved as images.
I would like something you can open easily and which opens the surface plot as though you had just run the code to generate it. So that you are free to rotate it by dragging the mouse.
Does this exist?
I only have IDL version 8.3.
If you 'Generate code/save data' (either via the toolbar at the bottom of the window or via the generateCode method with the SAVEDATA keyword), you get a .sav file that contains your plot. Either use the code generated to restore it, or use the generated code as an example to write your own.

PDF image is 1.3 MB, but after removing text in Illustrator becomes ~35 Mb (still vector in PDF)

I have created a figure for my scientific work using ggplot (an R package for plotting data). It's a scatterplot that contains ~25.000 data points in a normal x-y-style plot. Each data point has a border and a color fill. The output vector PDF is 1.3 Mb in size. Now, I would like to make some final adjustments regarding font size and text position and merge it with other panels in a bigger figure which I normally do in Illustrator. So I add/embed the scatterplot to the rest of my figures which nicely loads all elements correctly. However, when I then simply save this file as .ai or .pdf, the output will be more than ~30 Mb. How is it possible, that all elements are still preserved in the original (small) PDF, but after Illustrator it is inflated to much? It is critical for me to keep the file size small.
I tried many things, including different PDF exporting options in Illustrator and macOS Preview PDF file compression, but nothing worked. I even tried merging all those ~25.000 overlapping dots together in one or at least few shapes, but either Illustrator crashes in the process (Illustrator > Pathfinder unite/merge) or the resulting PDF shows some erratic behaviour, i.e. become black/white in Word (Illustrator > Flatten Transparency) What am I missing here?
Any help is appreciated!
When saving, make sure you're not enabling Illustrator editing capabilities. Leaving Illustrator editing capabilities enabled will essentially cause a copy of the Illustrator file (as an AI version) to be written into the PDF that's being saved. This often causes the PDF to increase dramatically in size, especially for files with many vector or path elements.
I had the same issue. What worked was me was this:
Export as eps instead of pdf in ggplot. You may need to use device=cairo_ps as an option (I did).
In Adobe Illustrator, create a new document and select the web option
Combine all your figures into this new figure by dragging and dropping them there. Use "Embed" to embed those figures into the new one.
Make all changes you need
Save as pdf with default options (I used preset {Smallest File Size (PDF 1.6)}.
This preserved the small file size for me. I think the only thing that matters here is the use of eps instead of pdf when exporting from ggplot.

Arrange multiple (32) .png files in a grid

I've been pulling my hair out for the past week trying to figure out elementary R coding but can't seem to get anywhere (haven't used R since 2013 not that its a great excuse).
All I want is a 4x8 grid made up of 32 .png files (maps I've made), and I want to do it without loading one image file at a time (http://www.statmethods.net/advgraphs/layout.html).
So I think I can load the images within the folder writing (please correct me if my beliefs are bs)
img <- list.files(path='c:/a',patt='compo[0-32].*',full.names=T)
Then I was thinking maybe in the lines of par(mfrow=c()), layout, grid.arrange (writing png plots into a pdf file in R), grid.raster (How to join efficiently multiple rgl plots into one single plot?) - which I've read up on and experimented with accordingly not resulting in anything worthwhile..
The latter I employed only with the following outcome
It made me giggle.
I don't really think lattice is the way to go anyway.
Any help would be greatly appreciated!
Another approach is to read the PNG images with readPNG then use grid and gridExtra:
library(png)
library(grid)
library(gridExtra)
plot1 <- readPNG('plot1.png')
plot2 <- readPNG('plot2.png')
grid.arrange(rasterGrob(plot1),rasterGrob(plot2),ncol=1)
Alternative: If you want to save the plot using ggsave, instead of grid.arrange you can use
tmp <- arrangeGrob(rasterGrob(plot1),rasterGrob(plot2),ncol=1)
ggsave('filename.png',tmp,width=12,height=5)
Not sure what your concern is about loading all the image files -- how else could you read their data to create the new image?
ETA: to load the files, I'd just use png::readPNG . One way to collect the images would be(12 images selected here)
filenames<-dir(pattern='compo')
foo<-list()
for(j in 1:12) foo[[j]]<-readPNG(filenames[j]
If you're willing to load them and use the base plot tools, then layout is the command you want. E.g., for 12 images loaded
layout(matrix(1:12,nr=4,byr=T))
for (j in 1:12) plot(foo[[j]])

Qt and exporting vector images

I am using QPainter to draw some graphs on the screen and save these graphs as a raster images.
Is it possible to use the same logic I used for rasterizing these graphs to have them exported to vector images that I can use in a PDF?
You can use a QSvgGenerator as a "backend" to your QPainter, and you'll get an SVG document out of it. (The documentation has an example for how to do that.)
I don't know if you can use that directly in PDFs, but there are converters out there if you need a different vector graphics format.

Resources