0

I want to set the fig.height in an RMD chunk based in the number of rows in the data. For this, I have just set

{r, fig.height = nrow(data)}

but this doesn't change the figure height at all. However, it works when I set

{r, fig.height = 25}

where 25 is the number of rows in the data. I wonder why that is since they should be the same thing, right? I need the fig.height to be set dynamically with nrow since I have multiple datasets with different number of rows. I've even tried setting first

height <- nrow(date)

in another chunk and then using it as the fig.height but this doesn't work either.

Using some toy data, here is a reproducible example.

pl <- mpg %>%
      ggplot(aes(x=class)) %>%
      geom_bar()

Printing the plot in a separate chunk with {r, fig.height = length(unique(mpg$class))} produces a plot with a different height than setting {r, fig.height = 7} so the problem is not only for nrow() for me. Using {r, fig.height = length(unique(mpg$class))} looks the same as when not setting any height.

Joe
  • 181
  • 7
  • I cannot reproduce your problem. It is working here with `{r, fig.heit = nrow(name_of_dataframe)}`. Are you correctly specifying the name of the `data.frame`? Can you try to produce a minimal example Rmd.file where this problem occurs? – TimTeaFan Apr 25 '23 at 10:47
  • Does this answer your question? [Dynamic height and width for knitr plots](https://stackoverflow.com/questions/15365829/dynamic-height-and-width-for-knitr-plots) – MKR Apr 25 '23 at 10:47
  • @MKR I saw that post and I defined the height in the same way in another chunk first but that didn't work. – Joe Apr 25 '23 at 10:49
  • @TimTeaFan Yes, I made sure that I used the correct name and the nrow(data) produces 25 as well. – Joe Apr 25 '23 at 10:57
  • @Joe: Ok, so the naming is not the problem, but to help you solve the issue, we need to understand how your Rmd file differes from a simple standard template. Maybe something is overwritting the `fig.height` argument? Have you tried to reproduce your problem with some toy data in a separte file? – TimTeaFan Apr 25 '23 at 11:09
  • @TimTeaFan I edited the question with a reproducible example with some toy data on a separate file. – Joe Apr 25 '23 at 11:25
  • 1
    Could you make it really easy on us and remove any doubts about your set-up by making your reproducible example a single copy/pasteable block that gives a complete Rmd file? The way it is now is like a recipe: put this code: `...` inside a code chunk with these settings `...`, and we don't have your header (are you using the default YAML header?) so there's still lots of doubt if we have the same set-up as you if we try to reproduce the problem. – Gregor Thomas Apr 25 '23 at 12:45

1 Answers1

0

In the examples below the figure height is different in both examples:

Example 1:

---
title: "Untitled"
output: html_document
date: "2023-04-25"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
library(ggplot2)
```

## R Markdown

```{r}
my_fig_height <- length(unique(mpg$class))
```

## Including Plots

```{r, echo=FALSE, fig.height=my_fig_height}
mpg %>%
      ggplot(aes(x=class)) +
      geom_bar()
```

Example 2:

---
title: "Untitled"
output: html_document
date: "2023-04-25"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
library(ggplot2)
```

## R Markdown

```{r}
my_fig_height <- length(unique(mpg$class))/2
```

## Including Plots

```{r, echo=FALSE, fig.height=my_fig_height}
mpg %>%
      ggplot(aes(x=class)) +
      geom_bar()
```
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39