R ggplot, remove white margins in ggsave/ggplot - r

How can I remove the white margins in ggsave?
My question is exactly the same as Remove white space (i.e., margins) ggplot2 in R. However, the answer there isn't ideal for me. Instead of trial and error for a fixed but unknown aspect ratio, I would like to give ggsave a height and weight and want my plot (ie top of title to bottom of x-label) to automatically expand to that configuration without white margin.
How can I remove the strange white margin around my .png (plotted with r, ggplot)? gives a way to make the margin transparent, but they are still there and the plot is smaller than height and width I set in the saved file.

Found the answer from Remove Plot Margins in ggplot2
theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
does the job

In this answer linking to this blog post there is a solution which also works for different aspect ratios. You can crop the image on your hard drive, independently of OS:
knitr::plot_crop()

If you're using Unix or Mac OS, another option when the various margin options aren't trimming enough is to use the pdfcrop command available within Unix through R's ability to invoke system commands:
# after saving image, run pdfcrop
system2(command = "pdfcrop",
args = c("name_or_path_of_file_before_crop.pdf",
"name_or_path_of_file_after_crop.pdf")
)
For more, see: https://robjhyndman.com/hyndsight/crop-r-figures/

If pdf and pdfcrop aren't your thing, for example you work in png with a png logo - then see my answer here: How to save a ggplot2 graphic with the proper aspect ratio?

I ended up adding a command like this after ggsave:
system("/usr/local/bin/mogrify -trim -border 8 -bordercolor white output.png")
-trim removes an existing margin and -border 8 -bordercolor white adds a small 8px margin around the plot.
For a plot that had a gray background, a few white pixels were left around the edges of the plot, so I used the -shave option to remove a few extra pixels:
system("/usr/local/bin/mogrify -trim -shave 4x4 output.png")

Related

Remove whitespace from a wordcloud plot

Below is a screenshot from quarto document with a wordplot I've generated using wordcloud package in R.
The challenge is that there is a lot of white space between the words on the plot and the border of the plot image.
How do I reduce the size of the extra 'white space'?
Reducing the white space might be way more complex than these two options! Hope that can help as well.
You can change the size of the letters with the wordcloud2 package and fill in the white space.
Example: wordcloud2(data=demoFreq, size=1.6)
You can save the image without the white space and upload it manually in Quarto.
Example: ![Image1](file_name.png)

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

How to remove the white margin of ggord in ggplot2?

I have this plot. I am using the following package and script to do a biplot. But the figure output always has white margins on the top and bottom of the actual figure. I tried removing them using plot.margin=unit(c(0,0,0, 0), but no luck. Any thoughts on how to do this or is it just the default and there is nothing that can be done?
The script (I am using the example, but the same issue with mine):
>library(devtools)
>library(ggord)
>ord <- prcomp(iris[, 1:4])
>p <- ggord(ord, iris$Species)
>p
The problem is the white margins on the top and bottom as seen here. How can I remove them?
The white space you are seeing is a function of the window size when you save the image. If you resize your window vertically or horizontally, you'll notice that the white space shrinks or grows.
If your goal is to save the plot as a PNG image, you can export it directly to file with your desired dimensions as follows:
png("plot1.png", 800, 300);
p;
dev.off();
Adjust the 800 to the desired length and the 300 to the desired height, in pixels. If the size is larger than the actual graph, it will pad with blank space.

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 make grids covered by figures?

I am new to RRDtool. I generated a graph with grid(--grid-dash 1:0), a LINE(LINE1:rt#4e9a06) and I also have the area between the line and the x-axis coloured (AREA:rt#4e9a06 ). I notice the grid still shows up in the colored-area. I am wondering if there is any way to cover the grid with the colored-area.
Also, I am also wondering if there is any good-looking rrdtool samples/examples available online? Thanks.
I have no way of testing this currently, but here's what the documentation says (emphasis mine) :
[-c|--color COLORTAG#rrggbb[aa]]
Override the default colors for the standard elements of the graph.
The COLORTAG is one of BACK background, CANVAS for the background of
the actual graph, SHADEA for the left and top border, SHADEB for the
right and bottom border, GRID, MGRID for the major grid, FONT for the
color of the font, AXIS for the axis of the graph, FRAME for the line
around the color spots, and finally ARROW for the arrow head pointing
up and forward. Each color is composed out of three hexadecimal
numbers specifying its rgb color component (00 is off, FF is maximum)
of red, green and blue. Optionally you may add another hexadecimal
number specifying the transparency (FF is solid). You may set this
option several times to alter multiple defaults.
What about making an almost transparent grid with arguments like these (note the extra 7F parameter which translates to 127 in decimal):
-c MGRID#<hex triplet>7F -c GRID#<hex triplet>7F
It should still be visible in the background but be invisible (or barely noticeable) once any graph covers it.
Note that this answer from the developer of RRDTool says that the grid is always painted after the graph, so in the end you'll always have it in the foreground, your only solution is to either totally disable it or tinker with color/transparency parameters to make it invisible when covered by the graphed data.

Resources