Reputation: 4444
When I knit a Tufte handout pdf the legends of some figures are often too large. I'm knitting a fig.fullwidth=TRUE
section and have varied the fig.width=
argument from 2 to 50 to see if they make any difference. When I specify fig.width=2
I end up with a small figure and the legend expands hugely to fill the space:
Obviously the next step was to try a larger fig.width
but these stopped making any difference after about fig.width=10
(which is still better though):
I assume the fig.width is the width in inches, so this makes sense as the paper size I'm using is A4 so about 8 inches.
My question is, how do I reduce the size of the legend further so it takes up a more appropriate size? I've tried manually setting the font sizes with:
theme(legend.title = element_text(size = 9),
legend.text = element_text(size = 8))
but these have had no affect to the knitr legend (although they do affect the plot when manually plotted as expected).
Minimal reproducible example (paste into a .Rmd file and knit):
---
title: "knitr: figure legend too big"
documentclass: article
classoption: a4paper
output: rmarkdown::tufte_handout
---
```{r setup, include=FALSE}
require("knitr")
require("rgdal")
require("rgeos")
require("maptools")
require("ggplot2")
```
```{r, fig.cap="test", fig.fullwidth=TRUE, fig.width=10}
# About 650k
download.file("https://census.edina.ac.uk/ukborders/easy_download/prebuilt/shape/England_gor_2011_gen.zip",
destfile = "regions.zip")
unzip("regions.zip")
regions <- readOGR(dsn = ".", "england_gor_2011_gen")
regions@data$test <- as.character(1:nrow(regions@data))
regions_f <- fortify(regions, region = "name")
regions_f <- merge(regions_f, regions@data, by.x = "id", by.y = "name")
ggplot() +
geom_polygon(data = regions_f, aes(long, lat, group = group, fill = test),
colour = "black") +
coord_equal() +
theme(legend.title = element_text(size = 9), # doesn't seem to make
legend.text = element_text(size = 8)) # a difference to knitr
```
As always, thanks for looking at this.
Upvotes: 4
Views: 2384
Reputation: 30104
I think that is due to the fact that maps have to set the aspect ratio to 1
. The default figure height in tufte::tufte_handout()
is 2.5, so even if the figure width is as large as 10, the actual map size is still like 2.5 x 2.5. When you increase fig.width
, you also need to increase fig.height
, e.g.
```{r, fig.cap="test", fig.fullwidth=TRUE, fig.width=6, fig.height=6}
Actually, when you only set fig.width=10
, knitr did generate a 10 x 2.5 PDF image, but rmarkdown has enabled the figure cropping feature by default, so the large white margins on the left and right of the figure are cropped, and you ended up with a 2.5 x 2.5 figure.
Upvotes: 5