Set ggsave() aspect ratio to the ratio that RStudio's "Zoom" button gives? - r

I am using ggsave() to save plots from ggplot().
In R-Studio, clicking the "Zoom" button automatically resizes most of my plots to have quite aesthetic proportions, whereas when I try to manually specify the size and aspect ratios, I get plots that don't look as nice.
Is there a way that I can make ggsave use the same aspect ratio or size that R-Studio's "Zoom" button does?
Or how do I call the Zoom button using code?

If you right click on the zoomed image and select 'Inspect element', you'll see the aspect ratio there. That can then be passed into ggsave. Note that I'm assuming the aspect ratio selected by R Studio's zoom is constant on a given monitor in which case you would only need to find the aspect ratio once.
Here's an example of where to find it.

Related

How to make sure the font stays the same when using ggsave

I'm trying to add multiple plots from ggplot into my Latex report. Most plots have different dimensions, as some need to be rectangle shaped, where as some need to be square shaped. To shape the plots, I've been changing the height and width parameters in ggsave. However, this also changes the font sizes. How do I have consistent font sizes for all my plots, and also be able to change the width and height of each plot separately?
I've already specified the font size in the plot using theme(text = element_text(size=5)), but it doesn't seem to actually stay consistent between the plots.

How to remove image margins when plotting and exporting square image with R?

I'm trying to plot a square TIFF image (1024 x 1024), but it displays it as a vertically stretched rectangle. I've tried adjusting margins in plotRGB and utilizing par(mar=c(0,0,0,0)), but I can't get the plot and output to be a square even though R recognizes the raster as a square.
Does anyone have any advice on how to simply fix this issue? I've attached two images, one of the original image and one of the image when plotted/exported in R. The goal of my program is to use histmatch solely to adjust the saturation of the source image to that of a matching reference image. The exported image thus cannot be vertically stretched, and must have no margin.
Screenshot of original image
R display and export of image

VMR9 aspect ratio mode

I want to disable the letter-boxing of a VMR9 with three input streams. The aspect ratio mode was correctly set to VMR_ARMODE_NONE with function IVMRWindowlessControl9::SetAspectRatioMode. But I can't get the VMR9 to give up letter-boxing. The black bars are always shown.
Does anybody now what could be the reason for this behavior?
BTW: With IVMRAspectRatioControl9::SetAspectRatioMode I can't disable the black bars too.

Set width and height of graphic made using ggplot, grid, and gridExtra

If I have a graphic composed of several plots, say three plots arranged vertically. This is a gtable object and can be drawn to the page with:
grid::grid.newpage()
grid::grid.draw(plot)
However I see that the plot in my RStudio is 'smushed up' as in the screenshot below:
As you can see in the bottom right corner it is squashed and the titles overlap with other elements of the graphic.
If I hit zoom and view the plot it is a lot bigger:
Now I know, that if I were to export my gtable plot using pdf() or png() and such devices, I can set a width and a height, and so just make it big enough such that the plot is not squashed.
However, instead of one of those graphic devices, I would like to use export.grid, from the gridSVG package to save it to an SVG file. But if I do
gridSVG::export.grid(plot)
Then the SVG file exported looks squashed as it does in the RStudio plot window.
So my question is, how can I manipulate the dimensions of the graphic so it is drawn to SVG without it looking squashed? I draw the plot initially with grid.newpage and grid.draw, I wonder perhaps I have to specify some size of the page or drawing using grid.
Thanks,
Ben.

How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio

I am using QGraphicsView/QGraphicsScene to display an image. The image is always displayed to its original size with scroll bars at the ends. I want QGraphicsView to fit the image automatically as per the the size of the window keeping aspect ratio.
I tried this but nothing happened:
ui->graphicsView->fitInView(0,0,ui->graphicsView->width(),ui->graphicsView->height(),Qt::KeepAspectRatio);
You are providing the rectangle of the view and not that of the scene.
This should work:
ui->graphicsView->fitInView(scene->itemsBoundingRect() ,Qt::KeepAspectRatio);
itemsBoundingRect calculates and returns the bounding rect of all items on the scene. So the graphicsview's view matrix will be scaled in order to fit the contents of the scene.
I would advise you to reimplement resizeEvent and have this call there as well.

Resources