Remove white spaces before and after an image - r

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?

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)

Why does Markdown in Jupyter Notebook represent these Horizontal Rules differently with the same input?

In my Juptyter notebook I have many horizontal rules (generated in markdown cells) to break up the blocks of code, to create the horizontal rule I am using 3 underscores "___". For some reason, which I cannot figure out, the line sometimes appears as dark grey and sometimes as light grey.
In the screenshots below you can see the same "___" produces two differently colour lines.
Does anyone know the reason? Ideally i'd like them all to be the darker grey
It appears that the horizontal line color flip-flops each time you use it.
So the first line will be black, the second line will be gray, the third line will be black, etc.
Since I am using the horizontal line as a separator, I put the horizontal line before and after each section.

Placement of pictures in markdown/bookdown

I am writing a text in bookdown with Rstudio and want to include a picture within the text like this:
Some text goes here
```{r, fig.cap="\\label{fig:figs}figlabel"}
knitr::include_graphics("images/image.png")
```
Some other text goes here.
However, when I render the book with bookdown::render_book("index.Rmd"), The inserted picture is placed on the next page rather than where it is placed in the text. I want it to be placed between the two sentences, but it is placed below the last one.
Is there a way to control where in the text the image is rendered? I have tried to look at chunk options for images, and also in the bookdown documentation, but neither seem to document ways to control placement of figures.
LaTeX will try to find an optimal location for the figure. You can force the placement of floating figures and tables with \FloatBarrier. Note that doing so, you might end up with a lot of white space on the bottom of the page.
Some text goes here. See fig. \#ref(fig:my-fig)
```{r my-fig, fig.cap="fig caption"}
knitr::include_graphics("images/image.png")
```
\FloatBarrier
Some other text goes here.

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.

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

Resources