Size of plots Knitr - r

I'm trying to change the size of a wordcloud but I don't know how to do this. Apparently lower values of fig.width make the plot bigger, not smaller, and vice versa. It seems to take up an enormous amount of space, compared to the second plot which is just the right size.
\documentclass{report}
\begin{document}
<<echo=FALSE,message=FALSE,warning=FALSE,fig.width=3>>=
library(wordcloud)
set.seed(1)
freq<-sample(letters,2000,prob=1:26,rep=T)
wordcloud(names(table(freq)),table(freq),min.freq=60,colors=brewer.pal(6,"Dark2"),scale=c(1,.2))
#
<<echo=FALSE,warning=FALSE>>=
library(ggplot2)
freq<-table(freq)
wf<-as.data.frame(freq)
wf$freq<-ordered(wf$freq,levels=wf$freq[order(wf$Freq)])
ggplot(subset(wf,Freq>60),aes(freq,Freq)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=45,hjust=1))
#
\end{document}

The arguments fig.width & fig.height provide aspect-ratio between the two axis: in order to indicate the size of the plot in your knitted / exported document, you should use both of them, ever (e.g., by using pandoc when knitting a rmarkdown file to a .docx).
Alternatively, the fig.asp = let you adjust the size of the image (e.g., multiply current size by 2 with fig.asp = 2). However, this method still provides a fig.height and fig.width parameters during the export (under the hood).

Related

How to reduce (white) margin of a waffle chart?

I' d like to ask some help on how to reduce the amount of white space in a waffle chart using the waffle package in R.
Here's the code:
waffle(table(dat$Age.Group),
rows=5,
title="Demographic Age Groups")
Then it shows this:
When I knitt it, it takes too much space, refer to the picture:
I'd like to put the text closer to the plot so it won't look awkward, and so I can fit more waffle charts in one page.
I really haven't tried anything, except changing the spacing between the squares, but that doesn't seem to be the problem. I've also looked up similar problems, but I can't find anything addressing to the removing of the margin.
Thank you so much in advance!
The issue is that waffle uses coord_equal under the hood. As a result the size of the plot is not adjusted to fill the size of the output device. If the size of the output device is larger than needed for the plot it is filled up with white space.
One option would be to set equal=FALSE in waffle. However, in that case you will no longer get squares but rectangles.
Second option would be to reduce the size or the height of the output device via chunk options, i.e. the size used by Rmarkdown or knitr to render your plot which by default is a 7 x 7 inches. To this end you could set fig.height or as an alternative set the aspect ratio using fig.asp:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r, include=FALSE}
library(waffle)
```
```{r, include=FALSE}
parts <- c(One=80, Two=30, Three=20, Four=10)
chart <- waffle(parts, rows=8, equal = FALSE)
```
```{r fig.cap="Default"}
chart + ggplot2::labs(title = "Default")
```
```{r fig.asp=.4, fig.width = 7, fig.cap="Set the aspect ratio"}
chart + ggplot2::labs(title = "Set the aspect ratio")
```
```{r fig.height = 2.8, fig.width = 7, fig.cap="Reduce the figure height"}
chart + ggplot2::labs(title = "Reduce the figure height")
```

R Markdown density plot error using kable [duplicate]

I have created a plot with ggplot2 where the x-axis labels are not readable unless the plot is larger than default. When viewing in Rstudio I am able to resize dynamically. When saving with ggsave() I am able to specify height and width. How would I do this within the Rmarkdown file so that the output contains a plot of the desired size?
You can specify height and width in the code chunks
```{r, fig.width=10,fig.height=11}
df %>% ggplot(aes(x = x, y = y)) + geom_point()
```
As a side-answer, note that you can also use metric-system units using ggplot2::unit():
library(ggplot2)
knitr::opts_chunk$set(fig.width=unit(18,"cm"), fig.height=unit(11,"cm"))
If you would like to do this for all plots then you can use the r setup Rmd chunk at the beginning of the file.
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)

R - How to change size of a whole figure produced by grid.arrange?

I want to display multiple ggplot objects in one figure using package gridExtra. It works, but titles are cut. I can fix it by changing plots font size, but I would rather change the width of the whole figure so that it is wide enough to fit in titles (and as wide as other outputs in my RMarkdown doc, e.g. the table right over it). I've tried to change margins by typing par(mai = 2*par('mai')) before call to grid.arrange, but it did nothing. Does anyone know how to do it properly? I am really confused about the gridExtra package.
library(gridExtra)
g <- grid.arrange(zad4_kl$pw, zad4_kl$pk, zad4_kl$pp, zad4_AC$pw, zad4_AC$pk, zad4_AC$pp, nrow = 2)
g
Screenshot of produced output
If you are saving the image, then you can increase the width and dpi in ggsave.
In R Markdown, you can use fig.width and fig.height
{r, echo=FALSE, fig.width=8, fig.cap="A nice image."}
library(gridExtra)
g <- grid.arrange(zad4_kl$pw, zad4_kl$pk, zad4_kl$pp, zad4_AC$pw, zad4_AC$pk, zad4_AC$pp, nrow = 2)
g
If rendered image doesn't fit the Markdown output, then you will have to change font in individual plots.

How to change size (width & height) of a plot produced by PerformanceAnalytics package in RStudio?

Here is the code:
charts.PerformanceSummary(dret_data, main='Portfolio Performance',
Rf=0.015/252, plot.engine='default')
Plot looks compressed in the notebook, snippet below (I would like to make it bigger):
When looking into the documentation nothing about plot resizing mentioned:
?charts.PerformanceSummary
Is there a way to work somehow around this issue and control witdh and height of the plot in the performanceAnalytics package in the R notebook?
If you're using an RStudio notebook for the code & output, you'll need to adjust the fig.width and/or fig.height in the chunk(or document) options.
Below is the difference between fig.height 3 & 5:
```{r fig.height=3}
library(PerformanceAnalytics)
charts.PerformanceSummary(managers)
```{r fig.height=5}
library(PerformanceAnalytics)
charts.PerformanceSummary(managers)
Edit:
Making the title larger:
# Keep original par settings for later
opar <- par()
#change cex.main to whatever you want. Normal is 1.2 I think
par('cex.main' = 2)
#Plot now has larger title
charts.PerformanceSummary(managers)
# reset par settings
par(opar)

Specify height and width of ggplot graph in Rmarkdown knitr output

I have created a plot with ggplot2 where the x-axis labels are not readable unless the plot is larger than default. When viewing in Rstudio I am able to resize dynamically. When saving with ggsave() I am able to specify height and width. How would I do this within the Rmarkdown file so that the output contains a plot of the desired size?
You can specify height and width in the code chunks
```{r, fig.width=10,fig.height=11}
df %>% ggplot(aes(x = x, y = y)) + geom_point()
```
As a side-answer, note that you can also use metric-system units using ggplot2::unit():
library(ggplot2)
knitr::opts_chunk$set(fig.width=unit(18,"cm"), fig.height=unit(11,"cm"))
If you would like to do this for all plots then you can use the r setup Rmd chunk at the beginning of the file.
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)

Resources