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)
Related
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")
```
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)
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.
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).
How can I modify the height and width of individual charts produced by ggplot2 in a knitr output pdf? In other words, not using the fig.height and fig.width in the code chunk options. I have some charts that work best at one size and others that need to be a different size.
Here is an example .Rmd file to show how to do this. It would be preferable to use a 'setup' chunk to loaded and attached namespaces, and to set options. Each chunk can have its own set of options which will take precedence over the options set by opts_chunk$set().
---
title: Change size of individual ggplot2 charts in knitr pdf file
output: pdf_document
---
It would be preferable to set chunk options only once, in a "setup" chunk.
Then, as needed, modify the options for each following chunk as needed. The
chunk options `include = FALSE, cache = FALSE` are helpful for the set up chunk
so that it will be evaluated every time the file is knitted, but will not create
any lines within the intermediate or final file.
```{r setup, include = FALSE, cache = FALSE}
library(ggplot2)
library(knitr)
opts_chunk$set(fig.width = 12, fig.height = 8)
```
The first figure will have the default size of 12 by 8 inches, although it is
scaled to to fit the pdf page.
```{r figure1}
ggplot(mpg) + aes(x = manufacturer, y = cty) + geom_boxplot() + coord_flip()
```
This figure can be made again, but with a different size.
```{r figure2, ref.label = "figure1", fig.width = 4, fig.height = 3}
```
A screenshot of the output pdf: