---
title: "reprex for issue with Rmarkdown and facetted ggplot with coord.fixed()"
author: "Chris Evans"
date: "2024-07-09"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, fig.width = 16, fig.height = 12, cache = TRUE)
library(tidyverse)
```

The issue is that I get a lot of unwanted white space above and below facetted plots when using `coord.fixed()` (and with `theme(aspect.ratio = 1)`) but not with plots without the facetting.

```{r makeData}
valN <- 2000
valCorr <- .7
matCov <- matrix(c(1, valCorr, valCorr, 1), byrow = TRUE, ncol = 2)
vecMeans <- c(0, 0)
MASS::mvrnorm(n = valN, mu = vecMeans, Sigma = matCov) %>%
  as_tibble(.name_repair = "universal") %>%
  rename(Pre = `...1`,
         Post = `...2`) %>%
  mutate(ID = row_number(),
         group = ID %% 2) -> tibDat
```

First a simple plot: no unnecessary white space.

```{r plot1}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  geom_point(alpha = .1) +
  geom_smooth() +
  xlim(c(-4, 4)) +
  ylim(c(-4, 4)) +
  coord_fixed()
```

Now facet the plot without `coord_fixed()`, no unwanted white space.

```{r}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  facet_grid(cols = vars(group),
             space = "free",
             ) +
  geom_point(alpha = .1) +
  geom_smooth() +
  xlim(c(-4, 4)) +
  ylim(c(-4, 4)) 
```

And then this which adds facetting and illustrates the additional white space appearing when I add `coord_fixed()`.

```{r}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  facet_grid(cols = vars(group),
             space = "free",
             ) +
  geom_point(alpha = .1) +
  geom_smooth() +
  xlim(c(-4, 4)) +
  ylim(c(-4, 4)) +
  coord_fixed()
```

There was a suggestion on the web, but about a slightly different issue about `scales = "free"` in the facetting that using `theme(aspect.ratio = 1)` was better than `coord_fixed()`.  That doesn't change things for me.

```{r}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  facet_wrap(facets = vars(group)) +
  geom_point(alpha = .1) +
  geom_smooth() +
  theme(aspect.ratio = 1)
```

Using `theme(plot.margin = margin(t = 0,  b = 0))` has no effect.

```{r}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  facet_grid(cols = vars(group),
             space = "free",
             ) +
  geom_point(alpha = .1) +
  geom_smooth() +
  xlim(c(-4, 4)) +
  ylim(c(-4, 4)) +
  coord_fixed() +
  theme(plot.margin = margin(t = 0,
                             b = 0))
```

Using `theme(plot.margin = margin(t = -70,  b = -70))` also has no effect.

```{r}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  facet_grid(cols = vars(group),
             space = "free",
             ) +
  geom_point(alpha = .1) +
  geom_smooth() +
  xlim(c(-4, 4)) +
  ylim(c(-4, 4)) +
  coord_fixed() +
  theme(plot.margin = margin(t = -70,
                             b = -70))
```

Set margin units explicitly with `theme(plot.margin = unit(c(-5, 5, -5, 5), "cm"))`, still no joy on the vertical space though it can be seen that the right and left margins are imposed correctly.

```{r}
ggplot(data = tibDat,
       aes(x = Pre, y = Post)) +
  facet_grid(cols = vars(group),
             space = "free",
             ) +
  geom_point(alpha = .1) +
  geom_smooth() +
  xlim(c(-4, 4)) +
  ylim(c(-4, 4)) +
  coord_fixed() +
  theme(plot.margin = unit(c(-5, 5, -5, 5), "cm"))
```