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

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.

Related

Plot legend text doesn't fit

I have problem with size of my legend. The values in this legend are very small like 2e-18 (an example below) and they take up a lot of space. What can I do in this situation?
You can play with the keywidth property, to make the legend take up more space, or set a smaller font via the label.theme setting (see the online help).

Is there a way to make a ggplot legend resize to the width and length of the viewport it is in?

I am building a grid of geom_treemaps. There are 2 plots, side by side in one row. I want to have the legend (a continuous color gradient) span underneath the length of the entire window. To do this, I am extracting the legend from another geom with get_legend(plot) and drawing it in its own viewport. When I draw the legend, it shows no regard for the constraints of the viewport. I can adjust the legend.key.width parameter in the theme() of the plot I extracted the legend from, but that process is very manual. Is there a way to coerce this legend to fit (specifically lengthwise) into a viewport?
Thank you for any responses.

Left aligning plots but keeping actual size

How to save multiple plots and keep their different actual-sizes on a same page (image below). arrangeGrob meets my requirement on keeping the actual size of each plot but it is not possible to align them left. plot_grid, ggarrange, and gtable provide horizontal aligning but these functions resize the plot.

R ggplot, remove white margins in ggsave/ggplot

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")

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.

Resources