increase png resolution when using saveHTML() {animation} in R - r

I am in need of a way to increase the resolution of the png files created by saveHTML().
Here is a dummy script of what I am trying to do where in reality plot() is a number of nested loops:
x<-y<-rep(1,10)
saveHTML( for (i in 1:10){
plot.new()
plot.window(xlim=c(0,10),ylim=c(0,10))
plot(x[i],y[i])
}
,ani.dev="png",img.name="test",htmlfile="test")
A few things I have tried:
1) increase the animation size using ani.options(ani.height,ani.width) but I only get a larger grainy image.
2) call png() device inside the saveHTML expression and set the resolution there, but ultimately I dont get any figures.
3) call a new windows() device for plotting and setting the window size, but again this does not increase the resolution.
The most straight forward work-around that I came across is to create hi-res pngs and animate using ffmpeg. But I am not ready to re-work my script just yet.
Has anyone found a way to increase png resolution inside the saveHTML() function?

Instead of passing ani.dev="png", you can pass ani.dev = function(...){png(res=75*grain,...)}, where grain is some number > 1. If you specify the options ani.height and/or ani.width and multiply these values by the same factor grain, then you effectively increase the pixel resolution of the output by this factor.
N.B.: the default resolution 75 above might be machine dependent, I did not see it documented.

Related

How to output always the same size (width/height) graph in R?

When I output the graph in R, the size (width and length) is always changing. So whenever I output graphs, I change the size by myself.
For example, I need a consistent graph size to insert into my document, but sometimes the default value of width and height is not the same when I try to export, particularly on different PC. How can I fix W:500. H:400 graph size all the time?
Many thanks!!
In order to get consistency, you should set the size of the graphics device yourself before plotting. For example, in Windows, you can use
windows(width=5.5, height=5, title="Controlled Size")
## NOW make your plot
to get a window that is always 5.5 inches wide by 5 inches high.
Other devices use minor variations for setting the size. You can check out other devices by looking at the help page help(Devices)
For example, bmp, jpeg, png and tiff allow you to set the number of pixels, not just the number of inches.

R: how to specify resolution/quality in animate package

The animate package in R has a function saveVideo that saves images as a video format.
From the documentation:
saveVideo(expr, video.name = "animation.mp4", img.name = "Rplot",
ffmpeg = ani.options("ffmpeg"), other.opts = if (grepl("[.]mp4$",
video.name)) "-pix_fmt yuv420p", ...)
and it seems the 'bit-rate' needs to be specified under other-opts.
I don't fully understand what bit-rate means here, and how it is supposed to be specified other than just trial and error, and how it relates to the resolution of the output.
In R, we also have the function png() which saves images, where one can specify width, height, and res. I'd like to do something similar here. Is there a guideline for how to choose bit-rate so that I get the desired size and quality for the video?
Have you taken a look at the documentation page for ani.options?
Among those that seem to answer your are ani.width, ani.height
, and ani.resto manually adjust the width, height, and resolution of your animation.

rgl plot3d and snapshot3d: how do I save with resolution higher than my monitor?

I am saving several plots, made with rgl's functions plot3d and snapshot3d. I need to save some at very high resolution, higher my screen.
When I use
par3d("windowRect" = c(x,y,w,h))
to set the window dimension just before calling snapshot3d it works but the resolution is limited to my monitor's width or height.
Is there a way to break this limit?
Thank you all for your time.
The solution I found to save in high resolution is to move away from the snapshot3d function and use rgl.postscript to save to a vector format like svg. This allows me to rasterize later with the resolution I want.
Example:
rgl.postscript("graph.svg", fmt="svg")
The function rgl.snapshot really grabs a snapshot from the device and I think there's no way to break screen's boundaries using this function unless, as Ben Bolker points out in the comments (thanks), a virtual frame buffer is used.

How to enlarge map/plot in R?

I plotted a map in R but when I export it the size is very small. How can I enlarge the map and still save it as a picture? (I know that I can save it as PDF and then it's a vector graphic - but I need to copy it to Power Point and also need a transparent background - I don't think that is possible with a PDF isn't it?)
As you can see here the map is way to small to use it in a Power Point slide:
If someone knows a good way to save it as vector graph that I can easily use in Power Point that would be perfect as well.
The png() function lets you specify width and height size in the default resolution of "px" and the defaults are 480 and 480. You can also supply a res argument in units of "ppi". If you have text you probably ought to specify point size >= 20 for legibility. I generally save my graphs as PDF and convert to PNG with an external program. However, the latest versions of PowerPoint will accept pdf formats. It is also possible to save as an .eps format.

How do I export a higher resolution image of a Mathematica Graph object?

How do I export a re-sized version of the output I get from a call to GraphPlot
(or TreePlot if they produce different output) to a jpg file?
Currently, I'm simply calling Export[file_name, G]
where G is the result from a call to something like GraphPlot.
I'm using Microsoft office picture manager to view the jpgs,
but re-scaling them there yields unsatisfactory results due to poor resolution
(the graph I'm trying to plot has strings as labels which can't be made out after rescaling this way). I would like to be able to choose the size/resolution of the rendered jpg.
As Simon already pointed out, don't use a raster-format for a vector-graphics. Instead, export you plot to e.g. a scalable vector graphics:
graph = GraphPlot[ExampleData[{"Matrix", "HB/can_292"}, "Matrix"]];
Export["graph.svg", graph]
The advantage is, that in such an image, you can still adjust and change lines, polygons and colors. And finally, you can export it to an image of arbitrary quality easily.
And remember, for Plots which contain lines, polygons, ... everything with sharp edges you should never use jpg. General speaking, this is a format for photographs only since its compression is made for reducing filesize in natural images. In those images you don't recognize the compression, in images with text, lines and polygons you certainly will notice the artefacts. Use png if possible. Take your browser and zoom into the above image.
You can set both image size and compression level of the exported file by doing something like
Export[file_name, G, ImageSize -> 1200, "CompressionLevel" -> 0]
The best way I find is to use ImageResolution property. It Increases the resolution of exported image but does not change the scale. Use it like this:
Export[ "image-file-name.png", image, ImageResolution -> 500 ]
Set the size of your graph before exporting it:
Graph[theGraph, ImageSize->2000]

Resources