Remove whitespace from a wordcloud plot - r

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)

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

How to get rid of the unwanted space around plotoutput in shiny?

enter image description hereI have 2 plot outputs that are shown uder each other (using 2 plotoutputs in shiny). But there is a whitespace around the plots which is unnecessary, and I could not get rid of it using css styling such as (padding=0). I would like to have the plots beneath each other with no space between.
I would appreciate any advice! Thanks

Remove white spaces before and after an image

I set echo = false in .Rnw file with knitr. The output is a graph with lots of white space around it. How do I decrease the white space so that the graph is not too far away from the main text?

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