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")
```
Related
It turns out that setting plot sizes on a Mac with retina display can be challenging. I am not referring to file sizes or resolutions that can be taken care of with the fig.retina setting (I have seen several questions about that), but the fact that the actual figure layout differs between the inline version of a an RMarkdown script and both an exported version with the same dimensions (using ggsave) as well as a knitted version. Even small figure dimensions appear gigantic inline on the screen while they export and knit correctly. So, the ratio between font size and "plot size" (or whatever it would be called) appears to change. If I run the code on an older (non-retina) Macbook or a PC, the inline figure sizes are as expected.
This is an example of what I mean. The specified figure dimensions were fig.width=4, fig.height=3.
Is there a way to get the inline graph dimensions match the knitted versions...?
Thanks y'all!
edit: code included, not sure how reproducible it is though, since it might only be relevant to users with retina or 4k displays:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
```{r, fig.width=4, fig.height=3}
Pdata <- data.frame("X" = 1:10, "Y" = 1:10) # some data
ggplot(data = Pdata,aes(x = X,y = Y)) +
geom_point() +
theme_bw(base_size = 20) +
labs(x = "Some stuff", y = "Some more stuff")
```
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:
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 am trying to plot two figures in one line and I want to increase the space between them. I searched this forum and a few other websites but none of the options I found seems to be working. Changing the mai, mar and oma values moved everything around but the space remains the same. How can I keep the figures as they are now (size wise) but increasing the gap between them?
Here is my code:
```{r echo=FALSE, fig.width=6, fig.height=6}
g.erd <- erdos.renyi.game(100, 150, type="gnm")
par(mfrow = c(1, 2), mai = c(1, 0.1, 0.1, 0.1))
plot(g.erd, layout=layout.circle, vertex.label=NA)
```
```{r echo=FALSE, fig.width=3, fig.height=3.5}
hist(degree(g.erd), xlab="Degree", ylab="Frequency", main="")
par(mfrow = c(1, 1))
```
and here is how my plot looks like right now: http://i.stack.imgur.com/V2Fc7.png
A 'hackish' solution in ggplot2 would be to put extra line spaces before the beginning of your second graph's title with \n like so:
ggtitle("\n\nPlot Title")
You can try adding markdown breaks between each chunk. <br>, like this:
```{r, echo=F}
plot(cars)
```
<br><br><br>
```{r, echo=F}
plot(cars)
```
Before:
After:
You can stack multiple <br> to achieve the desired gap you want.
This approach kind of works. It depends on why you want the different sizes, but you may be able to fiddle with the layout width and height parameters, or the par(mar=c() to get the spacing and size you want. You could also create a layout that have 3 plotting areas, and leave one blank, as a way to try to force the smaller histogram into the desired location (layout.show(layout(matrix(c(1,1,2,3),ncol=2)))).
```{r echo=FALSE, fig.width=6, fig.height=6}
library(igraph)
g.erd <- erdos.renyi.game(100, 150, type="gnm")
layout(matrix(c(1,2), ncol=2), width=c(1,1))
par(mar=c(1,1,1,1))
plot(g.erd, layout=layout.circle, vertex.label=NA)
par(mar=c(10,5,9,1))
hist(degree(g.erd), xlab="Degree", ylab="Frequency", main="")
```
Hope this helps. Good luck.
edit: I've changed the plotting code to approximate equal graph size, but it's just sort of a guess, and other folks might be able to offer a better solution.
I seem to have discovered an odd behaviour with the knit Word command in RStudio
This works:
```{r qplot, fig.width = 6, fig.height=6, message=FALSE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data = cars) + geom_smooth()
````
this does not work
```{r q plot, fig.width = 6, fig.height=6, message=FALSE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data = cars) + geom_smooth()
```
returning this message:
pandoc.exe: Could not find image `./test_files/figure-docx/q%20plot.png', skipping...
The issue seems to be with the name of the chunk (i.e. qplot vs. q plot). When there is a space in the chunk name the plot does not render.
It only seems to affect the rendering of Word documents. Rendering html works fine.
I'm using RStudio 0.98.1028 and R3.1.1 on windows 7.
Has anyone else encountered this behaviour?
update
a space after the chunk name also seems to elicit the same behaviour:
this does not work
```{r q_plot , fig.width = 6, fig.height=6, message=FALSE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data = cars) + geom_smooth()
```
Posting the solution in case someone runs across this in future.
From Ben Bolker in the comments Avoid spaces and periods . in chunk labels and directory names as stated in the knitr documentation http://yihui.name/knitr/options.
This error only seems to affect making plots using knitWord. Code chunks with labels that contain spaces and that don't have plotting commands render normally. knitHTML also seems to work fine regardless of if chunk labels have a space or not.
# Let's make a plot
```{r ugly plot}
plot(btc_prices)
```
should apparently be
# Let's make a plot
```{r ugly_plot}
plot(btc_prices)
```
So no spaces... otherwise you'll waste hours googling and crying.